给定一句英语,要求你编写程序,将句中所有单词的顺序颠倒输出。
输入格式:
测试输入包含一个测试用例,在一行内给出总长度不超过 80 的字符串。字符串由若干单词和若干空格组成,其中单词是由英文字母(大小写有区分)组成的字符串,单词之间用 1 个空格分开,输入保证句子末尾没有多余的空格。
输出格式:
每个测试用例的输出占一行,输出倒序后的句子。
输入样例:
Hello World Here I Come
输出样例:
Come I Here World Hello
利用栈先进后出的原理来实现逆序
#include<iostream>
#include<string.h>
#include<stack> using namespace std; int main(){
string str;
stack<string> ss;
while(cin>>str)
ss.push(str);
cout<<ss.top();
ss.pop();
while(!ss.empty()){
cout<<' '<<ss.top();
ss.pop();
}
return ;
}
普通查找空格后输出后一位
#include <stdio.h>
#include <string.h> int main(){
char c[];
gets(c+);
c[] = ' ';
for(int i=strlen(c);i>=;i--){
if(c[i] == ' '){
printf("%s", c+i+);
c[i] = '\0';
if(i == ){
printf("%c", '\n');
}
else{
printf("%c", ' ');
}
}
else{
continue;
}
}
return ;
}