Reverse Words in a String
Description:
Given an input string, reverse the string word by word.
For example,
Given s = "the sky is blue
",
return "blue is sky the
".
Code:
1 void reverseWords(string &s) { 2 istringstream sstr(s); 3 s = ""; 4 string str; 5 while (sstr>>str) 6 s = str + ' ' + s; 7 if (!s.empty()) 8 s.erase(s.end()-1);//删除多余的空格 9 }