数据结构,问题 A: 翻转字符串
题目描述
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.
Ignatius 喜欢用相反的方式写字。给定 Ignatius 编写的单行文本,您应该反转所有单词,然后输出它们。
输入
输入包含多个测试用例。输入的第一行是一个整数 T,它是测试用例的数量。T 测试用例紧随其后。 每个测试用例都包含一行和多个单词。一行最多有 1000 个字符。 输出
对于每个测试用例,您应该输出经过处理的文本。
输入
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.
输出
For each test case, you should output the text which is processed.
样例输入 复制
3
olleh !dlrow
m'I morf .udh
I ekil .mca
样例输出 复制
hello world!
I'm from hdu.
I like acm.
提示
Remember to use getchar() to read '\n' after the interger T, then you may use gets() to read a line and process it.
#include<bits/stdc++.h>
using namespace std;
stack<char> q;
void print(){
while(!q.empty()){
cout << q.top();
q.pop();
}
}
int main(){
int n;cin >> n;
cin.ignore();
while(n > 0){
string s;
getline(cin, s);
for(char c : s){
if(c == ' '){
print();
cout << ' ';
}else {
q.push(c);
}
}
print();
cout << '\n';
n --;
}
return 0;
}