-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstop_gninnips_my_sdrow.cpp
46 lines (34 loc) · 1.34 KB
/
stop_gninnips_my_sdrow.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
//Write a function that takes in a string of one or more words, and returns the same string, but with all five or more letter words reversed (Just like the name of this Kata). Strings passed in will consist of only letters and spaces. Spaces will be included only when more than one word is present.
//Examples:
//spinWords( "Hey fellow warriors" ) => returns "Hey wollef sroirraw"
//spinWords( "This is a test") => returns "This is a test"
//spinWords( "This is another test" )=> returns "This is rehtona test"
#include <string>
#include <vector>
#include <algorithm>
#include <iostream>
std::vector<std::string> split(const std::string& str, char delim);
std::string spinWords(const std::string &str);
std::string spinWords(const std::string &str)
{
std::string result = "";
for (std::string& word : split(str, ' ')) {
if (word.length() >= 5) {
word = std::string(word.rbegin(), word.rend());
}
result += word + " ";
}
result.pop_back();
return result;
}
std::vector<std::string> split(const std::string& str, char delim)
{
std::vector<std::string> strings;
size_t start;
size_t end = 0;
while ((start = str.find_first_not_of(delim, end)) != std::string::npos) {
end = str.find(delim, start);
strings.push_back(str.substr(start, end - start));
}
return strings;
}