厦大C语言上机 1487 单词个数统计

1487.单词个数统计


时间限制: 1000 MS          内存限制: 65536 K
        
提交数: 1073 (0 users)          通过数: 281 (273 users)


问题描述
做腻了数的题目,小明决定做做字符串处理的题目。这不,小明找到了这样一道题:输入一行字符,统计其中有多少个单词,单词之间用空格,逗号,或句号分隔开。


输入格式
长度不超过100000的一行字符,由空格,逗号,句号和字母组成。


输出格式
包含的单词数。


样例输入
Life is a journey, not a destination.


样例输出
7


来源

xmu

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

#define MAX_SIZE 100005

int main()
{
    char text[MAX_SIZE] = { 0 };
    int len, count = 0, is_word = 0, i;

    fgets(text, MAX_SIZE, stdin);
    len = (int)strlen(text);
    for (i = 0; i < len; ++i)
    {
        if (('a' <= text[i] && text[i] <= 'z') || ('A' <= text[i] && text[i] <= 'Z'))
        {
            if (!is_word)
            {
                is_word = 1;
                count++;
            }
        }
        else
            is_word = 0;
    }

    printf("%d\n", count);

    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值