单词的逆序输出 c语言实现

最近在CODIA刷题遇到了这样的题目,看到大家都在用递归解题一点也不符合菜鸟的做题风范,我就按照自己的想法凑了一下答案,仅供参考。

题目描述

输入一句话(不含标点),其中包含多个单词并以空格分隔,请把这句话改为“倒装句”,即单词出现的顺序前后颠倒(注意是单词顺序不是字母顺序,单词之间有空格)。输出该“倒装句”。

输入描述

  • 1≤单词数目≤20
  • 单词长度≤10

列入输入字符串:what is wrong with you 输出为:you with wrong is what

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

void main()
{
    char sentence[100];
    int i, count=0, maxs = 1;
    gets(sentence);
    // scanf("%[^\n]", sentence);
    for(i=0;i<strlen(sentence);i++)
    {
        if(sentence[i] != ' ' && count != -1)
        {
            count++;
            if(count>10)
            {
                count = -1;
                break;
            }
        }
        else
        {
            maxs++;
            count = 0;
        }
    }
    if(maxs>20 || strlen(sentence)<0 || count==-1)
        printf("The sentense word num is not satify the requirement or some word length is too long!\n");
    else
    {
        int length = strlen(sentence);
        int printindex = 0;
        for(int j = length-1;j>=-1;j--)
        {
            if(sentence[j] == ' ' || j == -1)
            {
                printindex = j+1;
                for(;printindex<length;printindex++)
                {
                    printf("%c", sentence[printindex]);
                }
                printf(" ");
                length = j;
            }
        }
    }    
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值