Final Ugly English | ||||||
| ||||||
Description | ||||||
ACM twist-toy encountered such a problem in the work, this article, to ensure that this article only lowercase letters and spaces, please send the articles in each word inverted output, such as "hello world this is an competition". You should output "olleh dlrow siht si na noititepmoc". | ||||||
Input | ||||||
A group of data, each line of data input from the lower case letters and spaces of the article, the length of not more than one thousand | ||||||
Output | ||||||
Output a line for each word after the reversal of the article. | ||||||
Sample Input | ||||||
hello world this is an competition | ||||||
Sample Output | ||||||
olleh dlrow siht si na noititepmoc | ||||||
Source | ||||||
"尚学堂杯"哈尔滨理工大学第七届程序设计竞赛 |
题意:将一句话中每个单词反转,单词间可能不止一个空格,输出结果最后也不应带空格。
#include<bits/stdc++.h>
using namespace std;
const int N = 1000 + 10;
int main()
{
char a[N];
while(gets(a))
{
stack<char>k;
int l=strlen(a);
for(int i=0; i<=l; i++)
{
if(a[i]==' '||i==l)
{
while(!k.empty())
{
printf("%c",k.top());
k.pop();
}
if(i!=l)
printf(" ");
}
else
{
k.push(a[i]);
}
}
printf("\n");
}
}