天天看点

HDU1062:Text Reverse

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

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.

   
    
        

//这道题本身也不难,关键在于找空格而已

#include <stdio.h>
#include <string.h>

int main()
{
    int n,i,j,len,a,b;
    char str[1000],s[1000][1000],t;

    scanf("%d",&n);
    getchar();
    while(n--)
    {
        gets(str);
        memset(s,'\0',sizeof(s));
        a = b = 0;
        len = strlen(str);
        for(i = 0;i<len;i++)
        {
            if(str[i] == ' ')
            {
                a++;
                b = 0;
                continue;
            }
            s[a][b] = str[i];//用s数组来存放每个空格隔断的字符串
            b++;
        }
        for(i = 0;i<=a;i++)
        {
            len = strlen(s[i]);
            for(j = 0;j<len/2;j++)//字符串的反转
            {
                t = s[i][j];
                s[i][j] = s[i][len-j-1];
                s[i][len-j-1] = t;
            }
            printf("%s",s[i]);
            if(i!=a)//输出空格
            putchar(' ');
        }
        printf("\n");
    }

    return 0;
}