填空题
1. 请补充main()函数,该函数的功能是:从字符串str中取出所有数字字符,并分别计数,然后把结果保存在数组tt中并输出,把其他字符保存在tt[10]中。
例如,当str1="00237481367539dfji"时,结果为:
0:2 1:1 2:1 3:3 4:1 5:1 6:1 7:2 8:1 9:1 other charactor:4
注意:部分源程序如下。
请勿改动main()函数和其他函数中的任何内容,仅在横线上填入所编写的若干表达式或语句。
试题程序: #include<stdlib.h>
#include<stdio.h>
#include<conio.h>
void main()
{
int i,tt[11];
char*str="00237481367539dfji";
char*p=str;
system("CLS");
printf("***the origial data***\n");
puts(str);
for(i=0;i<11;i++)
tt[i]=0;
while(*p)
{
switch(______)
{
case '0':tt[0]++;break;
case '1':tt[1]++;break;
case '2':tt[2]++;break;
case '3':tt[3]++;break;
case '4':tt[4]++;break;
case '5':tt[5]++;break;
case '6':tt[6]++;break;
case '7':tt[7]++;break;
case '8':tt[8]++;break;
case '9':tt[9]++;break;
______
}
______
}
printf("****the result****\n");
for(i=0;i<10;i++)
printf("\n%d:%d",i,tt[i]);
printf("\nother charactor:%d",tt[i]);
}
【正确答案】
1、* p
default: tt[10]++;
p++;
【答案解析】 由整个switch语句可知,该语句的功能为判断字符串str中的每一个字符,因此,第一空处填“*p”;按照题目的要求,将所有非数字字符放在tt[10]中,因此,第二空处填“default: tt[10]++;”;指针变量p指向字符串str中的每一个字符,每一次循环时,p向后移动一个位置,因此,第三空处填“p++;”。