给定一个字符串,逐个翻转字符串中的每个单词。
输入: "the sky is blue"
输出: "blue is sky the"
- 利用istringstream函数
class Solution {
public:
string reverseWords(string s) {
istringstream words(s);
stack<string> st;
string res;
string word;
while(words >> word) {
st.push(word);
}
while (!st.empty()) {
string tmp = st.top(); st.pop();
if (st.size() != 0)
res += tmp + " ";
else
res += tmp;
}
return res;
}
};