#include <iostream> #include<cstdio> #include <stack> #include <string> using namespace std; int main() { string str = "the sky is blue"; //reverseWords(str); string fs=""; stack<string> strs; int len=str.length()-1; int begin=0; int end=0; while(begin<=len&&end<=len) { while(str[begin]==' ') begin++; end=begin; while(str[end]!=' ') { if(end==len) break; end++; } if(end!=len) { end--; } string temp=str.substr(begin,end-begin+1); strs.push(temp); end++; begin=end; if(begin==len) break; } while(!strs.empty()) { fs+=strs.top(); fs+=" "; strs.pop(); } fs=fs.substr(0,len+1); cout<<fs<<endl; cout<<fs.length(); system("pause"); return 0; }