改错题
1. 下列给定程序中,函数proc()的功能是:将字符串tt中的大写字母都改为对应的小写字母,其他字符不变。
例如,若输入“I,am,A,Student”,则输出“i,am,a,student”。
请修改程序中的错误,使它能得到正确结果。
注意:不要改动main()函数,不得增行或删行,也不得更改程序的结构。
试题程序:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<conio.h>
//****found****
char proc(char str[])
{
int i;
for(i=0;str[i];i++)
{
//****found****
if((str[i]>='a')&&(str[i]<='z'))
str[i]+=32;
}
return(str);
}
void main()
{
char str[81];
system("CLS");
printf("\nPlease enter a string:");
gets(str);
printf("\nThe result string is:\n%s",
proc(str));
}
【正确答案】(1)错误:char proc(char str[])
正确:char *proc(char str[])
(2)错误:if((str[i]>='a')&&(str[i]<='z'))
正确:if((str[i]>='A')&&(str[i]<='Z'))
【答案解析】 由主函数中的函数调用及函数proc()的返回值可知,函数proc()的返回值类型为字符型指针,因此,“char proc(char str[])”应改为“char *proc(char str[])”;题目要求将所有的大写字母转化为小写字母,因此,“if((st[i]>='a')&&(str[i]<='z')”应改为“if((st[i]>='A')&&(str[i]<='Z'))”。