Word Reversal

Description

For each list of words, output a line with each word reversed without changing the order of the words.

 

Input

 

The first line contains a positive integer indicating the number of cases to follow. Each case is given on a line containing a list of words separated by one space, and each word contains only uppercase and lowercase letters.

 

Output

 

For each test case, print the output on one line.

 

Sample Input

3
I am happy today
To be or not to be
I want to win the practice contest

Sample Output

I ma yppah yadot
oT eb ro ton ot eb
I tnaw ot niw eht ecitcarp tsetnoc



题目意思:将每个单词的字符进行反转。


最初代码:
#include<stdio.h>
#include<string.h>
int main()
{
    int n,m,i,j,k;
    char s[1000],x[1000];
    scanf("%d",&n);
    getchar();
    while(n--)
    {
        gets(s);
        m=strlen(s);
        k=0;
        for(i=0; i<=m; i++)
        {
            if(s[i]!=' '&&s[i]!=0)
            {
                x[k]=s[i];
                k++;
            }
            else
            {
                for(j=k-1; j>=0; j--)
                    printf("%c",x[j]);///逆向输出
                if(s[i]!=0)
                    printf(" ");
                k=0;
            }
        }

        printf("\n");
    }

    return 0;
}
 
 

这个代码是一个数组向另外一个数组中倒数,时间复杂度上还是存在着问题,修改之后的新代码如下:

 1 #include<stdio.h>///时间复杂度更小的另外一种方法
 2 #include<string.h>
 3 int main()
 4 {
 5     int t,m,i,j;
 6     char s[10000];
 7     scanf("%d",&t);
 8     getchar();
 9     while(t--)
10     {
11         gets(s);
12         m=strlen(s);
13         for(i=0; i<m; i++)
14         {
15             if(s[i]==' ')
16             {
17                 for(j=i-1; j>=0&&s[j]!=' '; j--)
18                 {
19                     printf("%c",s[j]);
20                 }
21                 printf(" ");
22             }
23         }
24         for(i=m-1; i>=0&&s[i]!=' '; i--)
25         {
26             printf("%c",s[i]);
27         }
28         printf("\n");
29     }
30     return 0;
31 }

 

 



转载于:https://www.cnblogs.com/wkfvawl/p/8733862.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值