HDU1062 Text Reverse(栈 c++)
题目链接: Text Reverse
题目大意: 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.(翻转字符串)
样例输入:
3
olleh !dlrow
m'I morf .udh
I ekil .mca
样例输出:
hello world!
I'm from hdu.
I like acm.
题解思路:
代码如下:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int ans;
string str;
int m, n;
stack<char> a;
char ch;
void slove()
{
}
int main()
{
cin >> m;
getchar();
while(m--){
while(1){
ch = getchar();
if(ch == ' ' || ch == '\n' || ch == EOF){
while(!a.empty()){
cout << a.top();
a.pop();
}
if(ch == ' '){
cout <<" ";
}
}
else{
a.push(ch);
}
if(ch == '\n' || ch == EOF){
break;
}
}
cout <<"\n";
}
return 0;
}