矫正单词

1.题目:

Problem Description

LJ 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
LJSZ
nil eij ihs !uhz
edam yb .fyb

Sample Output

ZSJL
lin jie shi zhu!
made by byf.

  Author

byf

 

2.注意事项:

      这题主要要注意一个字符串中有多个空格的情况,还有最后的换行的处理。

 

3.参考代码:

 

 

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

int main()
{
    int t, i, j, l, x, y;
    char s[1000];

    while (~scanf("%d%*c", &t)) {

        while (t--) {

            gets(s);

            l = strlen(s);

            x = 0;
            y = 0;

            for (i = 0; i < l; i++) {

                if (s[i] == ' ') {   ///这种情况是有空格的情况

                    y = i;

                    for (j = y - 1; s[j] != ' ' && j >= 0; j--)
                        printf("%c", s[j]);

                    printf(" ");
                } else {   ///这是没有空格的情况
                    x++;
                }
            }

            if (x == l) {   ///没有空格就逆序输出整个字符串

                for (i = l - 1; i >= 0; i--)
                    printf("%c", s[i]);

            } else {   /// 否则处理最后的'\0'的情况

                for (j = l - 1; s[j] != ' '; j--)
                    printf("%c", s[j]);

            }

            printf("\n");
        }
    }

    return 0;
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值