翻转句子中单词的顺序

来源:互联网 发布:scratch live for mac 编辑:程序博客网 时间:2024/06/05 09:59

题目:输入一个英文句子,翻转句子中单词的顺序,但单词内字符的顺序不变。句子中单词以空格符隔开。为简单起见,标点符号和普通字母一样处理。

例如输入“I am a student.”,则输出“student. a am I”。

分析:将句子的每个单词拆分开,存入栈中,然后依次从栈中取出。

#include<iostream>#include<string>#include<algorithm>#include<stack>using namespace std;int main(){string text="Life is full of confusing and disordering Particular time, a particular location, Do the arranged thing of ten million time in the brain, Step by step, the life is hard to avoid delicacy and stiffness No enthusiasm forever, No unexpected happening of surprising and pleasing So, only silently ask myself in mind Next happiness, when will come?";stack<string>s;string word;int i=0;while(i<text.length()){while(text[i]==' ')i++;word="";while(text[i]!=' ' && text[i]!=0){word+=text[i];i++;}if(word.length()>0){s.push(word);}}cout<<s.top()<<" ";s.pop();while(!s.empty()){cout<<" "<<s.top();s.pop();}return 0;}


0 0
原创粉丝点击