填空题 给定程序中fun函数的功能是:分别统计字符串中大写字母和小写字母的个数。
例如,给字符串s输入:AAaaBBb123CCccccd,则应输出结果:upper=6,lower=8。
请改正程序中的错误,使它能计算出正确的结果。
注意:不要改动nlain函数,不得增行或删行,也不得更改程序的结构!
给定源程序:
#include<stdio.h>
/**********found**********/
void fun(char *s, int a, int b)
{
while(*s)
{ if(*s>="A"&&*s<="Z")
/**********found**********/
*a=a+1;
if(*s>="a"&&*s<="Z")
/**********found**********/
*b=b+1;
s++;
}
}
main()
{ char s[100]; int upper=0, lower=0;
printf("/nPlease input a string:"); gets(s);
fun(s, &upper, &lower);
printf("/n upper=%d lower=%d/n", upper, lower);
}
【正确答案】
【答案解析】(1)void fun(char *s, int *a, int *b)
(2)*a=*a+1;
(3)*b=*b+1; [解析] (1)由主函数中调用fun函数的语句fun(s, &upper, &lower)可知,函数的后两个变量为指针的形式,所以用*a和*b。
(2)*a的作用是用来记录大写字母的个数,此处的作用是对*a累加1,所以此处应改为*a=*a+1。
(3)*b的作用是用来记录小写字母的个数,此处的作用是对木b累加1,所以此处应改为*b=*b+1。