填空题
1. 请补充函数proc(),该函数的功能是:按'0'到'9'统计一个字符串中的奇数数字字符各自出现的次数,将结果保存在数组num中(注意:不能使用字符串库函数)。
例如,输入“x=1581453927843770hfds574”,结果为:1=2,3=2,5=3,7=4,9=1。
注意:部分源程序如下。
请勿改动main()函数和其他函数中的任何内容,仅在函数proc()的横线上填入所编写的若干表达式或语句。
试题程序:
#include<stdlib.h>
#include<stdio.h>
#define M 1000
void proc(char *str,int num[])
{
int i,j;
int bb[10];
char *p=str;
for(i=0;i<10;i++)
{
num[i]=0;
bb[i]=0;
}
while(______)
{
if(*p>='0'&&*p<='9')
______;
p++;
}
for(i=1,j=0;i<10;i=i+2,j++)
______;
}
void main()
{
char str[M];
int num[10],k;
system("CLS");
printf("\nPlease enter a char string:");
gets(str);
printf("\n**The original string**\n");
puts(str);
proc(str,num);
printf("\n**The number of letter**\n");
for(k=0;k<5;k++)
{
printf("\n");
printf("%d=%d",2*k+1,num[k]);
}
printf("\n");
}
【正确答案】
1、*p
bb[*p-'0']++
num[j]=bb[i]
【答案解析】 由函数proc()可知字符型指针变量p指向字符串str,要统计一个字符串中的奇数数字字符各自出现的次数,需要检查字符串str中的每一个字符,因此,第1空处填“*p”。数组bb中存放奇数数字字符各自出现的次数,每检查到一个奇数数字字符,数字相应的元素值加1,因此,第2空处填“bb[*p-'0']++”。按照题目中的要求,结果保存在数组num中,并返回给主函数,因此,第3空处填“num[j]=bb[i]”。