简答题
C语言
题号 #2257674
请编写函数proc(),它的功能是:求出str所指字符串中指定字符的个数,并返回此值。
例如,若输入字符串123412341123,输入字符4,则输出2。
注意:部分源程序给出如下。
请勿改动main()函数和其他函数中的任何内容,仅在函数proc()的花括号中填入所编写的若十语句。
试题程序:
#include<stdlib.h>
#include<conio.h>
#include<stdio.h>
#define N 8l
int proc(char*str,char c)
{
}
void main()
{ char a[N],ch;
system("CLS"):
printf("\nPlease enter a strin9:");
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中存放字符串中指定字符的个数,最后返回给主函数。
