您的位置:首页 > 其它

HDU 1062 TEXT Reverse(文本反转)

2016-07-15 20:54 393 查看
Text Reverse
Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u
Submit Status Practice HDU
1062

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.

Hint

Remember to use getchar() to read '\n' after the interger T, then you may use gets() to read a line and process it.

 

解题思路:每遇一个空格,就表示一个单词,所以从第一个不是空格的字母且紧邻其后是空格或结束符的字母开始逆序输出。

注意:第一个for循环里的j必须在循环外赋初值0,否则输出单词会丢失!

#include<stdio.h>
#include<string.h>
int main()
{
char a[1010],b[1010];
int i,j,n,l,k;
scanf("%d",&n);
getchar();
while(n--)
{
gets(a);
l=strlen(a);
j=0;
for(i=0; i<l; i++)
{
if(a[i]!=' '&&(a[i+1]==' '||a[i+1]=='\0'))
{
k=i+1;
while(k--&&a[k]!=' ')
b[j++]=a[k];
}
if(a[i]==' ')
b[j++]=' ';
}
b[j]='\0';
for(i=0; b[i]!='\0'; i++)
printf("%c",b[i]);
printf("\n");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息