问题 问答题

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


void main ()

char a[M], ch;
system ("CLS");
printf (" \ nPlease enter
string: ");
gets (a);
printf ("\nPlease enter a char:");
ch=getehar ();
printf ("\nThe number of the char
is:% d\n", fun(a, ch));

答案

参考答案:

int fun(char *ss, char c)

{

int i=0;

for(;*ss!=’\0’;ss++)

if(*ss==c)

i++;/*求出ss所指字符串中指定字符的个数*/

return i;

}

解析:

本题考查:for循环语句遍历字符串,并通过if条件语句,判断字符串是否结束。

[解题思路] 从字符串中查找指定字符,需要使用循环判断结构,循环语句用来遍历字符串,循环条件为字符串没有结束,即当前字符不是’\0’,判断语句用来判断当前字符是否为指定字符。最后返回指定字符的个数。

选择题
单项选择题