c 语言 单词个数统计

/*****
题目描述
从键盘输入一行字符,长度小于1000。统计其中单词的个数,各单词以空格分隔,且空格数可以是多个。

输入
输入只有一行句子。仅有空格和英文字母构成

输出
单词的个数

样例输入 Copy
stable marriage problem Consists of Matching members
样例输出 Copy
7
*****/
这里注意一下最后一个单词的后面是不是空格就行了

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

int main()
{
    char str[1001];
    int i,count,l;
    gets(str);
    count = 0;
    for (i = 0; str[i] != '\0'; i++)
    {
        if(str[i] != ' '&&str[i+1] == ' ')
        {
            count++;
        }

    }
    l = strlen(str);
    if(str[l-1] != ' ')
        count ++;
    printf("%d",count);
    return 0;
}

发布了84 篇原创文章 · 获赞 0 · 访问量 1822

猜你喜欢

转载自blog.csdn.net/qq_39345244/article/details/104875649