题目描述:
请编写函数fun,该函数的功能是:统一一含字符串中单词的个数,作为函数值返回。一行字符串在主函数中输入,规定所有单词由小写字母组成,单词之间由若干个空格格开,一行的开始没有空格。
详细代码:
#include<stdio.h>
int fun(char* s);
int main()
{
char a[50] = "love you du , and all of you.";
printf("单词的个数是%d", fun(a));
}
int fun(char* s)
{
int count = 0;
while (*s)
{
if ((*s>='a'&&*s<='z')&&(*(s+1)==' '|| *(s + 1) == ',' ||* (s + 1) =='\0' || *(s + 1) == '.' || *(s + 1) == ';'))
{
count++;
}//注意此处判断遍历一个单词结束的条件。
s++;
}
return count;
}
/*此段代码的运行环境为VS2019*/