another kata

This commit is contained in:
2019-11-24 20:43:54 +01:00
parent 8fe92e6543
commit d668f6fdd4

View 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);
}