天天看點

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;
}