请补充函数proe(),该函数的功能是:按'0'到'9'统计一个字符串中的奇数数字字符各自出现的次数,结果保存在数组mum中(注意:不能使用字符串库函数)。
例如,输入“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[iJ=0;
}
while(【1】)
{
if(*p>='0'&&*p<='9')
【2】 ;
p++;
}
for(i=1,j=0;i<10;i=i+2,j++)
【3】 ;
}
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【2】bb[*p-'0 ']++【3】num[j]=bb[i]
【解析】由函数proc可知字符型指针变量P指向字符串str,要统计一个字符串中的奇数数字字符各自出现的次数,需要检查字符串str中每一一个字符,因此,【1】处填“*p”。数组bb中存放奇数数字字符各自出现的次数,每检查到一个奇数数字字符,数字相应的元素值加1,因此’【2】处填“bb L*p-'0']++”。按照题目中要求,结果保存在数组num 中返回到主函数当中,因此,【3】处填“num[j]=bb[i]”。