问答题 请编写函数proc(),它的功能是:求出str所指字符串中指定字符的个数,并返回此值。
例如,若输入字符串12341234123,输入字符4,则输出2。
注意:部分源程序给出如下。
请勿改动main()函数和其他函数中的任何内容,仅在函数proc()的花括号中填入所编写的若干语句。
试题程序:
#include<stdlib.h>
#include<conio.h>
#include<stdio.h>
#define N 81
int proc(char*str, char c)
{

}
void main()
{char a[N], ch;
system("CLS");
printf("/nPlease enter a string:");
gets(a);
printf("/nPlease enter a char:");
ch=getchar();
printf("/nThe number of the char is: %d/n", proc(a, ch));
}
【正确答案】
【答案解析】int proc(char *str, char c)
{
int i=0; //i是放与c相同的字符的个数的变量
for(; *str!="/0"; str++)
if(*str==c) //str所指字符串中指定字符的个数
i++;
return i;
} [解析] 求出str所指字符串中指定字符的个数,可以通过将str所指字符串中每一个字符与指定字符相比较,变量i中存放字符串中指定字符的个数,最后返回给主函数。