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     }

 

posted @ 2015-06-21 17:38  Rosanne  阅读(169)  评论(0编辑  收藏  举报