华为机试题:句子逆序

来源:互联网 发布:天猫商城的网络推广 编辑:程序博客网 时间:2024/05/20 14:19

题目描述:
将一个英文语句以单词为单位逆序排放。例如“I am a boy”,逆序排放后为“boy a am I”所有单词之间用一个空格隔开,语句中除了英文字母外,不再包含其他字符
输入描述:

将一个英文语句以单词为单位逆序排放。

输出描述:

得到逆序的句子。

输入例子:

I am a boy

输出例子:

boy a am I

#include <iostream>#include <stack>#include <string>using namespace std;int main(){    stack<string> str;    string s;    while(cin>> s){        str.push(s);    }    while(!str.empty()){        string c = str.top();  //返回栈顶元素,但不将元素弹出栈        cout << c ;        str.pop();           //删除栈顶元素,但不返回该元素值        if(!str.empty())            cout << ' ';    }    cout << endl;    return 0;}
0 0
原创粉丝点击