问题描述:
Given an input string, reverse the string word by word.
示例:
Given s = "the sky is blue
",
return "blue is sky the
".
可将字符串分割成子串数组,最后再将字串数组reverse即可。
过程详见代码:
class Solution {
public:
void reverseWords(string &s) {
if(s == "") return;
stringstream ss(s);
string t = "";
string res="";
while (ss >> t)
{
res = t + " " + res;
}
if(t != "")
res.pop_back();
s = res;
}
};