写在前面
虽然这和书上的算法差不多,但还是要注意一下字符函数的使用
[Submit][Status][Web Board]
Description
给一个句子,统计这个句子中有多少个单词。单词可能包含大写字母、小写字母、数字和其他符号,单词之间用空白符或标点符号隔开。
Input
有多组数据,每个句子占一行,句子长度不超过1000个字符,到文件尾结束。
Output
每个句子对应一个整数,代表这个句子中有多少个单词,每个整数占一行
Sample Input
Meep....meep!
How are you?
Sample Output
2
3
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<ctype.h>
#include<string.h>
int main()
{
char str[100];
int i,count=0,word=0;
char c;
while(gets(str)!=NULL)
{
for(i=0;(c=str[i])!='\0';i++)
{
if(isspace(c)!=0||ispunct(c)!=0)//是否为字符或空白符
word=0;
else if(word==0)//前面是字符且单词数为1
{
word=1;//重置单词数,相当于一个计数器
count++;//真实储存单词数的变量
}
}
printf("%d\n",count);
count=0;
}
return 0;
}