计算形参s所指字符串中包含的单词个数,作为函数值返回

#include<stdio.h>
#include<string.h>
int fun(char *s)
{
	int count=0;
	int flag=0;     //声明标志
	while(*s!='\0')
	{
		if(*s!=' ' && flag==0)
		{
			count++;
			flag=1;
		}
		if(*s==' ')   //当字符串出现空格,将flag置0,则可进入第一个if语句,单词个数加1
			flag=0;
		s++;
	}
	return count;
}
void main()
{
	char s[81];
	printf("Please input a string:");
	gets(s);
	printf("The string have %d word\n",fun(s));
}

  

posted @ 2019-07-28 18:15  石乐智先生  阅读(834)  评论(0编辑  收藏  举报