4104:单词翻转

4104:单词翻转

总时间限制: 
1000ms
 
内存限制: 
65536kB
描述

输入一个句子(一行),将句子中的每一个单词翻转后输出。

输入
只有一行,为一个字符串,不超过500个字符。单词之间以空格隔开。
输出
翻转每一个单词后的字符串,单词之间的空格需与原文一致。
样例输入
hello world
样例输出
olleh dlrow
代码:
#include<stdio.h>
#include<string.h>
int main()
{
    char str[501],anti_str[501];
    int i,j=0,t;
    gets(str);//输入要用gets函数
    for(i=0;str[i]!='';i++)
    {
        if(str[i]!=' ')//扫描整个字符串序列,如果未遇到空格,将当前单词存放到数组翻转单词中
        {
            anti_str[j]=str[i];
            j++;
        }
        else//否则逆序输出当前单词
        {
            for(t=j-1;t>=0;t--)
            {
                printf("%c",anti_str[t]);
            }
            printf(" ");
            j=0;
        }
    }
    for(t=j-1;t>=0;t--)//处理最后一个单词
    {
        printf("%c",anti_str[t]);
    }
    printf("
");
    return 0;
}
 
以大多数人努力程度之低,根本轮不到去拼天赋~
原文地址:https://www.cnblogs.com/gcter/p/7405990.html