倒排单词

编写程序,读入一行英文(只包含字母和空格,单词间以单个空格分隔),将所有单词的顺序倒排并输出,依然以单个空格分隔。

输入格式

输入为一个字符串(字符串长度至多为100)。

输出格式

输出为按要求排序后的字符串。

输入样例:

I am a student

输出样例:

student a am I


//巧妙
 1 #include <iostream>
 2 
 3 using namespace std;
 4 
 5 int main(){
 6 
 7     string res, str;
 8     while( cin >> str)  //本地可以可以通过 ctrl + z 快捷键 再按 enter 键(回车)结束输入,这样就可以输出结果了~
 9     {
10         res = str + ' ' + res;
11     }
12 
13     cout << res;
14 
15     return 0;
16 
17 }

 

//

string 数组

 

 1 #include <iostream>
 2 
 3 using namespace std;
 4 
 5 int main(){
 6 
 7     string s[100];  //string 数组,  即每一个元素 都可存一个字符串
 8 
 9     int n = 0;
10     while(cin >> s[n]) n ++;
11 
12     for(int i = n-1; i >= 0 ; i--) cout << s[i] << ' ';
13 
14 
15     return 0;
16 
17 }

 

单词逆置,栈这个数据结构

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 stack<string>q;
 4 string a;
 5 int k;
 6 int main()
 7 {
 8     while(cin>>a)
 9     {
10         if(a==" ")break;
11         q.push(a);
12     }
13     while(q.size())
14 {
15     cout<<q.top()<<" ";
16     q.pop();
17 }    
18     return 0;
19 }
View Code

 

 
posted @ 2021-01-12 12:09  Leo-aiolia-bao  阅读(138)  评论(0)    收藏  举报