Reverse Words in a String
Given an input string, reverse the string word by word.
For example,
Given s = “the sky is blue”,
return “blue is sky the”.
class Solution {
public:
void reverseWords(string &s) {
int index = 0;
string temp_str = "";
while(EOF != (index = s.find_first_of(' ')))
{
temp_str.insert(0, s.substr(0,index));
temp_str.insert(0, " ");
s = s.erase(0, index+1);
}
temp_str.insert(0, s);
s = temp_str;
}
};
int main()
{
string test = "the sky is blue";
Solution().reverseWords(test);
printf("result test: %s\n", test.c_str());
}