Text Reverse |
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) |
Total Submission(s): 9276 Accepted Submission(s): 2512 |
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 <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;
}