str为一个字符序列。请补充函数proc(),该函数的功能是:查找str中值为X的元素,返回该字符序列中值为X 的元素个数,并把这些值为X的元素下标依次保存在数组bb中。例如,在“abcdedabcdab”中查找“C”,结果为:2个“c”,下标依次为2、8。
注意:部分源程序给出如下。
请勿改动main()函数和其他函数中的任何内容,仅在函数proc()的横线上填入所编写的若干表达式或语句。
试题程序:
#include<stdlib.h>
#include<stdio.h>
#include<conio.h>
#define M 20
int bb[M];
int proc(char*str,char ch)
{
int i=0.n=0:
char t=ch:
char*p=str;
while(*p)
{
if(【1】)
【2】 ;
p++;
i++:
}
return 【3】 ;
}
void main()
{
char str[M];
char ch;
int i,n;
system("CLS"):
printf("***Input the original string***\n");
gets(str);
printf("***The Original***\n");
puts(str);
printf("***Input character***\n");
scanf("%C",&ch);
n=proc(str,ch)。
printf("\nThe numbr of character is:%d\n",n);
printf("***The suffix of character
***\n");
for(i=0;i<n;i++)
printf("%d",bb[i]);
}
参考答案
答案解析
【1】*p==t【2】bb[n++]=i【3】n
【解析】要查找字符串中的指定元素,就要将字符串中每一个元素与指定字符相比较,因此,【1】填“*p==t”;数组bb中存放与指定字符相同的元素的下标,每找到一个数组bb增加一个元素,因此,【2】填“bb[n++]=i”;变量n为数组bb的下标,表示字符串中与指定字符相同的元素个数,返回给主函数,因此,【3】填“n”。