求一道c语言题目的编写
来源:百度知道 编辑:UC知道 时间:2024/07/04 06:54:17
5.设计程序,输入一个字符串,通过调用一个有返回值的函数int count(char *s),统计字符串中出现空白字符的次数。例如:串“a b c”中空白字符有2个,则函数返回值为2。
#include<stdio.h>
#include<conio.h>
int count(char *s)
{
int i=0;
while(*s!='\0')
{
if(*s++==' ') i++;
}
return i;
}
void main()
{
char s[20]={NULL};
printf("please input a string: \n");
gets(s);
printf("出现空白字符的次数%d.",count(s));
getch();
}
int
count(char *s)
{
int num = 0;
char *p = s;
while(*p)
{
if (*p == ' ')
num++;
p++;
}
return num;
}