下列给定函数中.函数proc()的功能是:统计字符串中各元音字母(即A,E,I,0,U)的个数。注意:字母不分大小写。例如,输入There are two books,则应输出是1 2 0 3 0。
请修改程序中的错误,使它能得出正确的结果。
注意:不要改动main()函数,不得增行或删行,也不得更改程序的结构。
试题程序:
#include<stdlib.h>
#include<conio.h>
#include<stdio.h>
//****found****
proc(char*str.int num[5])
{
int k,i=5;
for(k=0;k<i.k++)
//****found****
num[i]=O;
for(;*str;slr++)
{
i=-1:
//****found****
switch(str)
{
case 'a ':case'A':{i=0;break)
case'e':case'E':{i=1;break;)
case'i':case'1':{i=2 break;)
case'o':case 'O':{i=3;break;)
case'u':case'U':{i=4;break;)
}
if(i>=O)
num[i]++;
}
}
void main()
{
char s1[81];int num1[5],i;
system("CLS");
printf("\nPlease enter a string:");
gets(s1);
proc(sl,huml);
for(i=0;i<5;i++)printf("%d",num1[i]);
printf("\n");
}
参考答案
答案解析
(1)错误:proc(char*str,int num[5])
正确:void proc(char*str,int num[5])
(2)错误:num[i]=0;
正确:num[k]=0;
(3)错误:switch(str)
正确:switch(*str)
【解析】由主函数中可知,调用函数无需返回值,因此,“proc(char*str,int num[5])”应改为“void Droc(char* str,int num[5])”;由函数proc()可知。变量k为数组num 的下标,因此,“num[i]=0;”应改为“num[k]=0;”;判断字符串str中每一个字符是否为元音字符,switch(str)判断的是str指向的地址,因此,应改为“switch(*str)”.