给定程序中,函数fun的功能:计算出形参s所指字符串中包含的单词个数,作为函数值返回。便于统计,规定各个单词之间用空格隔开。
#include<stdio.h>
int fun(char *s)
{
int n = 0, flag = 0;
while (*s != '\0')
{
if (*s != ' '&&flag == 0)
{
++n;
flag = 1;
}
if (*s == ' ')flag = 0;
++s;
}
return n;
}
int main()
{
char str[81];
int n;
printf("\nEnter a line text:\n");
gets(str);
n = fun(str);
printf("\nThere are %d words in this text.\n\n", n);
getchar();
return 0;
}