标签:style blog http io ar color os 使用 sp
Given an input string, reverse the string word by word.
For example,
Given s = "the sky is blue
",
return "blue is sky the
".
#include<iostream> #include<string> #include<sstream> #include<stack> using namespace std; class Solution { public: void reverseWords(string &s) { if(s.empty()) return; stringstream ss(s); s.clear(); stack<string> st; string tmp; while(ss>>tmp) { st.push(tmp); } while(!st.empty()) { tmp=st.top(); st.pop(); if(!st.empty()) s+=tmp+‘ ‘; else s+=tmp; } } }; int main() { Solution s; string ss=" the sky is blue "; cout<<ss.length()<<endl; cout<<ss<<endl; s.reverseWords(ss); cout<<ss.length()<<endl; cout<<ss<<endl; }
运行结果:
运行结果:
标签:style blog http io ar color os 使用 sp
原文地址:http://www.cnblogs.com/wuchanming/p/4110002.html