fun()功能:
统计一行字符串中单词的个数,作为函数值返回。
#include<stdion.h>
#include<string.h>
#define N 20
void main(){
char line[N];
int num=0;
printf("please enter a sting:\n");
gets(line);
num=fun(line);
printf("The number of the string is :%d\n",num);
}
int fun(char *s){ //传参
int i,j=0; //j为字符串个数
for(i=0;s[i]!="\0";i++){
if(s[i]!=''&&(s[i+1]==''||s[i+1]=='\0'))
j++;
}
return j;
}
最后心得:整个过程还是比较简单的,如果一个字母的下一个字符为空格或者为结束标记,则表示一个单词结束。