被调用函数原型:void CountChar(char *str, char ch,int *counter),str是指向字符串的指针,ch是被统计的字符,counter是保存字符ch个数的指针。
输入提示信息:"Input string:\n"
"Character to be counted:\n"
输入格式: 用gets()函数
输出格式:"The numbers of %c is %d.\n"
运行样例
Input string:
you are my sunshine.
Character to be counted:
e
The numbers of e is 2.
#include<stdio.h>
void countchar(char*str, char ch, int *counter);
int main()
{
char s[31];
char ch;
int counter=0;
printf("input string:\n");
gets(s);
printf("character to be counter:\n");
ch = getchar();
countchar(s,ch,&counter);
printf("the number of %c is %d.\n",ch,counter);
return 0;
}
void countchar(char *str,char ch, int *counter)
{
int i;
for(i=0;str[i]!='\0';i++)
{
if(str[i]==ch)
(*counter)++;
}
}