计算出形参s所指字将串中包含的单词个数,作为函数值返回。
为便于统计,规定各单词之间用空格隔开。
列如:形参s所指的字符串为:This is a c language program.,函数的返回值为6。
#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("Enter a line text:");
gets(str);
n = fun(str);
printf("There are %d words in this text.", n);
}