[二级C试题天天练]C语言考试试题4

全国等级考试资料网 2019-01-23 14:12:34 55
2. 改错题
下列给定程序中,函数fun()的功能是:依次取出字符串中所有的字母,形成新的字符串,并取代原字符串。
请改正程序中的错误,使它能得到正确结果。
注意:不要改动main函数,不得增行或删行,也不得更改程序的结构。
试题程序:
#include 
#include 
void fun(char *s)
{
 int i,j;
 for(i=0,j=0; s[i]!= ’’; i++)
/**********************found***********************/ 
     if((s[i]>= ’a’&&s[i]<= ’z’)&&(s[i]>= ’a’&&s[i]<= ’z’))
        s[j++]=s[i];
/**********************found***********************/
 s[j]= "";
}
main()
{
 char item[80];
 clrscr();
 printf(" enter a string: ");
 gets(item);
 printf(" the string is:%s ",item);
 fun(item);
 printf(" the string of changing is :%s ",item);
}

2. 改错题
(1)错误:if((s[i]>= ’a’&&s[i]<= ’z’)&&(s[i]>= ’a’&&s[i]<= ’z’))
     正确:if((s[i]>= ’a’&&s[i]<= ’z’)||(s[i]>= ’a’&&s[i]<= ’z’))
(2)错误:s[j]= "";
正确:s[j]=’’;
【解析】错误1:字母包括小写字母和大写字母,这里是"或"的关系,所以用"||"运算符。
错误2:字符串的结束标志符为字符,而不是字符串。 相关资料

相关阅读