统计一个英文句子中含有英文单词的个数,单词之间用空格隔开。
#include <stdio.h>
#define SPACE ' '
int count_word(char* str)
{
if (str == NULL)
return -1;
int count = 0;
char* p = str;
while (*p++ != '\0')
if (*p == SPACE)
count++;
count++;
return count;
}
int main()
{
char str[100];
fgets(str, 100, stdin);
int count = count_word(str);
printf("单词个数为:%d\n", count);
return 0;
}
运行结果: