填空题
1. 下列给定程序中,函数fun的功能是:删除指针p所指字符串中的所有空白字符(包括制表符、回车符及换行符)。
输入字符串时用“#”结束输入。
请改正程序中的错误,使它能输出正确的结果。
注意:不要改动main函数,不得增行或删行,也不得更改程序的结构!
试题程序:
#include<string.h>
#include<stdio.h>
#include<ctype.h>
fun (char *p)
{
int i, t;char c[80];
/*********found*********/
For(i=0,t=0;p[i];i++)
if(!isspace(*(p+i)))
c[t++]=p[i];
/*********found*********/
c[t]="\0";
strcpy(p,c);
}
void main()
{
char c,s[80];
int i=0;
printf("Input a string:");
c=getchar();
while(c!='#')
{s[i]=c;i++;c=getchar();}
s[i]='\0';
fun(s);
puts(s);
}
【正确答案】
1、(1)for(i=0,t=0;p[i];i++)
(2)c[t]='\0';
【答案解析】
该题目考查C语言关键字的书写,C语言中关键字是区分大小写的。另外为字符串结尾添加结束符时应书写为'\0',而非"\0","\0"表示一个字符串。该程序的if条件中应用了isspace函数,该函数的功能是检查ch是否为空格、跳格符(制表符)或换行符。