实验6-10 统计单词的长度

实验6-10 统计单词的长度 (15 分)

本题目要求编写程序,输入一行字符,统计每个单词的长度。所谓“单词”是指连续不含空格的字符串,各单词之间用空格分隔,空格数可以是多个。

输入格式:

输入给出一行字符。

输出格式:

在一行中输出每个单词的长度。每个数字后有一个空格。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main( ) {
    char a[100];
    gets(a);
    int count=0,i=0,len=strlen(a);
    while(i<=len){
        if(a[i]!=' '&&a[i]!='\n'&&a[i]!='\0'){//****关键******* 
            count++;
            i++;
        }
        else{
            printf("%d ",count);
            count=0;
            i++;
        }
    }
    return 0;
}


版权声明:本文为weixin_49592304原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。