hdu1062——Text Reverse

来源:互联网 发布:先锋网络电视破解版 编辑:程序博客网 时间:2024/05/16 04:43

原题:

Problem 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
3olleh !dlrowm'I morf .udhI ekil .mca
 


 

Sample Output
hello world!I'm from hdu.I like acm.

 

分析:

水题~

原码:

#include<stdio.h>#include<string.h>int main(){    int n,i,j,flag;    char str[1005];    scanf("%d",&n);    getchar();    while(n--)    {        gets(str);        flag=0;        int l=strlen(str);        for(i=0; i<l; i++)        {            if(str[i]==' ')            {                for(j=i-1; j>=flag; j--)         //不用真的翻转,倒序输出就行,当然string使用reverse函数也行                {                    printf("%c",str[j]);                }                printf(" ");                flag=i+1;            }        }        for(j=l-1; j>=flag; j--)        {            printf("%c",str[j]);        }        printf("\n");    }    return 0;}


 

原创粉丝点击