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.
InputThe 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.
OutputFor each test case, you should output the text which is processed.
Sample Input3
olleh !dlrow
m’I morf .udh
I ekil .mcaSample
Output
hello world!
I’m from hdu.
I like acm.
HintRemember to use getchar() to read ‘\n’ after the interger T, then you may use gets() to read a line and process it.
#include<iostream>
#include<cstring>
#include<stack>
using namespace std;
int main()//我其实只是进行了栈操作的基本运用,来逆转字符串
{
int n;
cin>>n;
getchar();//必不可少,清除缓存区的回车
string a;
while(n--)
{
getline(cin,a);
stack<char> s;
for(int i=0;i<a.length();i++)
{
if(a[i]==' ')
{
while(!s.empty())
{
cout<<s.top();
s.pop();
}
if(i==a.length()-1)
cout<<" "<<endl;
else
cout<<" ";
}
else if(i==a.length()-1)
{
s.push(a[i]);
while(!s.empty())
{
cout<<s.top();
s.pop();
}
cout<<endl;
}else
s.push(a[i]);
}
}
}