another kata
This commit is contained in:
35
Reverse words/reverse_words.cpp
Normal file
35
Reverse words/reverse_words.cpp
Normal file
@@ -0,0 +1,35 @@
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <iostream>
|
||||
|
||||
std::vector<std::string> split(std::string input, std::string delimiter = " "){
|
||||
ulong pos = 0;
|
||||
std::vector<std::string> result;
|
||||
while((pos = input.find(delimiter)) != std::string::npos){
|
||||
result.push_back(input.substr(0, pos));
|
||||
input.erase(0, pos + delimiter.length());
|
||||
}
|
||||
// Append remaining element!
|
||||
result.push_back(input);
|
||||
return result;
|
||||
}
|
||||
|
||||
std::string reverse_words(std::string str)
|
||||
{
|
||||
std::vector<std::string> words = split(str);
|
||||
std::vector<std::string> reversed_words;
|
||||
for(auto word: words){
|
||||
std::string reversed(word.rbegin(), word.rend());
|
||||
reversed_words.push_back(reversed);
|
||||
}
|
||||
std::string result = reversed_words[0];
|
||||
for(int i = 1; i<reversed_words.size(); i++){
|
||||
result += " " + reversed_words[i];
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
int main(){
|
||||
std::string mystring = "This is just an example string to check splitting.";
|
||||
std::cout << reverse_words(mystring);
|
||||
}
|
||||
Reference in New Issue
Block a user