1133: 单词个数统计
Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 3604 Solved: 2047
Submit Status Web Board
Description
从键盘输入一行字符,长度小于1000。统计其中单词的个数,各单词以空格分隔,且空格数可以是多个。
Input
输入只有一行句子。仅有空格和英文字母构成
Output
单词的个数
Sample Input
stable marriage problem Consists of Matching members
Sample Output
7
HINT
Source
#include<stdio.h>#include<string.h>
#define size 1000
void main ()
{
char str[size]={'\0'};
int count=0,len,i;
// printf("please input the string\n");
gets(str);
len=strlen(str);
for (i=0;i<len;i++)
{//判断是不是空格,不是的话在while里面,i++执行判断到下一个空格的出现或是结束
//'\0'字符串结尾的标志
if(str[i] != ' ')
{
count++;
while ( str[i] != ' ' && str[i] != '\0')
{
i++;
}
}
}
printf("%d\n",count);
}