题目
输入一行字符,统计其单词的个数,单词之间用空格进行分隔。
输入要求:读入的字符串应包含有空格
输出要求:”There are is %d in teh line.\n”
输入输出样例:
Input sample:
The C program language.
Output sample:
There are is 4 in teh line.
思路
单词个数为空格数+1,但是如果在第一个单词前出现空格的话那个空格需要剔除。
代码
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(){
int i,count=0;
char str[100];
gets(str);
for(i=0;i<strlen(str);i++){
if(str[i]==' '&&i!=0){
count++;
}
}
printf("There are is %d in teh line.\n",count+1);
return 0;
}