您的位置:首页 > 编程语言 > C语言/C++

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

2018-01-14 11:25 369 查看
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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: