关闭

Text Reverse

标签: 杭电入门指针
311人阅读 评论(0) 收藏 举报
分类:

Text Reverse

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 27   Accepted Submission(s) : 6

Font: Times New Roman | Verdana | Georgia

Font Size:  

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.

Author

Ignatius.L


对于输入的一行中的字符,每次遇到空格或者回车,就把与之前一个空格之间的字符倒序输出,做的时候总觉得用指针想起来好想一点,不过还是很烦啊、、
#include <stdio.h>
#include <string.h>

#define M 1007

int n;

int main()
{
    char ss[M],*pi,*pj,*pp;
    scanf("%d",&n);
    getchar();
    while(n--)
    {
        gets(ss);
        pi=&ss[0]; pj=&ss[0]; pp=&ss[0];
        while(*pi!='\0')
        {
            if(*pj==' ')
            {
                pp=pj;
                while(pp!=pi)
                {
                    pp--;
                    printf("%c",*pp);

                }
                printf("%c",*pj);
                pj++;
                while(*pj==' ')
                {
                    printf("%c",*pj);
                    pj++;
                }
                pi=pj; pp=pj;
            }
            if(*pj=='\0'||*pj=='\n')
            {
                pp=pj;
                while(pp!=pi)
                {
                    pp--;
                    printf("%c",*pp);
                }
                pi=pj; pp=pj;
            }
            pj++;
        }
        printf("\n");
    }
    return 0;
}

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:12362次
    • 积分:8
    • 等级:
    • 排名:千里之外
    • 原创:25篇
    • 转载:0篇
    • 译文:0篇
    • 评论:0条
    文章分类