给定一个字符串,逐个翻转字符串中的每个单词。

    1. 输入: "the sky is blue"
    2. 输出: "blue is sky the"
    • 利用istringstream函数
      1. class Solution {
      2. public:
      3. string reverseWords(string s) {
      4. istringstream words(s);
      5. stack<string> st;
      6. string res;
      7. string word;
      8. while(words >> word) {
      9. st.push(word);
      10. }
      11. while (!st.empty()) {
      12. string tmp = st.top(); st.pop();
      13. if (st.size() != 0)
      14. res += tmp + " ";
      15. else
      16. res += tmp;
      17. }
      18. return res;
      19. }
      20. };