1133: 单词个数统计
题目描述
从键盘输入一行字符,长度小于1000。统计其中单词的个数,各单词以空格分隔,且空格数可以是多个。
输入
输入只有一行句子。仅有空格和英文字母构成
输出
单词的个数
样例输入 Copy
stable marriage problem Consists of Matching members
样例输出 Copy
7
来源/分类
#include<stdio.h>
#include<math.h>
#define N 1000
/*
统计单词的个数
stable marriage problem Consists of Matching members
要注意第一个字符是否为空格
字母的前边是空格
*/
int main(){
char s[N],count=0;
gets(s);
if(s[0]!=' ') count=1;
for(int i=1;s[i]!='\0';i++){
if(s[i]!=' '&&s[i-1]==' ') count++;
}
printf("%d\n",count);
return 0;
}