Problem 1133: 单词个数统计
题目来源:
http://acm.zzuli.edu.cn/problem.php?id=1133
题目描述
从键盘输入一行字符,长度小于1000。统计其中单词的个数,各单词以空格分隔,且空格数可以是多个。
输入
输入只有一行句子。仅有空格和英文字母构成
输出
单词的个数
样例输入
stable marriage problem Consists of Matching members
样例输出
7
题意描述:
输入一个字符串,判断有几个单词。
解题思路:
一个非空格后是空格意味单词结束;注意若最后一个单词后无空格记得算上最后一个单词。
程序代码:
#include<stdio.h>
#include<string.h>
int main()
{
char str[1000];
int i,len,count=0;
gets(str);
len=strlen(str);
for(i=0;str[i]!='\0';i++)
{
if(str[i]!=' '&&str[i+1]==' ')
count++;
}
if(str[len-1]!=' ')
count++;
printf("%d\n",count);
return 0;
}
错误分析:
考虑不周,最后一个单词没算上。