C语言编程>第十六周 ⑦ s是全部由小写字母字符和空格字符组成的字符串,由len传入字符串的长度,请补充fun函数,该函数的功能是:统计字符串s中的单词数,结果由变量len传回。

例题:s是全部由小写字母字符和空格字符组成的字符串,由len传入字符串的长度,请补充fun函数,该函数的功能是:统计字符串s中的单词数,结果由变量len传回。每个单词之间都由空格隔开,并且字符串s开始不存在空格。

例如,s=“welcome welcome”,结果为:len=2。
请勿改动主函数main与其它函数中的任何内容,仅在fun函数的横线上填写所需的若干表达式或语句。

代码如下:

#include<stdio.h>
#define M 100
void fun(char*s,int*len)
{
    
    
	int j,n=0;
	for(j=0;j<*len;j++)
		if(s[j]>='a'&&s[j]<'z'&&s[j+1]==' '||s[j+1]=='\0')
			n++;
	*len=n;
}
main()
{
    
    
	char s[M];
	int len=0;
	printf("Enter a string:\n");
	gets(s);
	while(s[len])
		len++;
	fun(s,&len);
	printf("The lenber of word is:%d\n\n",len);
}

输出运行窗口如下:
在这里插入图片描述

越努力越幸运!
加油,奥力给!!!

猜你喜欢

转载自blog.csdn.net/qq_45385706/article/details/112273856