Problem Description
Ignatius likes to write words in reverse way. Given a single line of text which is written by Ignatius, you should reverse all the words and then output them.
Input
The input contains several test cases. The first line of the input is a single integer T which is the number of test cases. T test cases follow.
Each test case contains a single line with several words. There will be at most 1000 characters in a line.
Output
For each test case, you should output the text which is processed.
Sample Input
3
olleh !dlrow
m'I morf .udh
I ekil .mca
Sample Output
hello world!
I'm from hdu.
I like acm.
#include<stdio.h>//在做的时候没有考虑到 最后一个单词后面没有空格
#include<string.h>
char s[1002];
int main() {
int test,i,j,len,k;
scanf("%d",&test);
getchar();
while(test--) {
gets(s);
len=strlen(s);
for(i=0,k=0; i<len; i++) {
if(s[i]==' ') {
for(j=i-1; j>=k; j--) {
printf("%c",s[j]);
}
printf(" ");
k=i+1;
}
}
for(j=len-1; j>=k; j--)
printf("%c",s[j]);
printf("\n");
}
return 0;
}
#include <iostream>
#include <stack>
#include <stdio.h>
using namespace std;
int main() {
int n;
cin>>n;
getchar(); //因为下面要读一字符 需要消除换行符的影响
char ch;
while(n--) {
//string temp;
//getline(cin,temp);
stack<char> s1; //利用栈的特性 先进后出
while(1) {
ch = getchar();
if(ch==' '||ch=='\n'||ch==EOF) { //如果遇到空字符(一个单词) 或换行符 (一行的结束) 或输入结束 即打印出栈的内容
while(!s1.empty()) {
cout<<s1.top();
s1.pop();
}
if(ch=='\n'||ch==EOF) { //一定要有 判断是不是输入结束
break;
}
cout<<" ";
} else {
s1.push(ch); //否则的话 就不断进行压栈操作
}
}
cout<<endl;
}
return 0;
}