简答题 C语言
题号 #2258354

下列给定程序中,函数proc()的功能是:删除字符串str 中所有空白字符(包括Tab字符、Enter符及换行符)。输入字符串时以“#”结束输入。
请修改程序中的错误,使它能得出正确的结果。
注意:不要改动mainI)函数,不得增行或删行,也不得更改程序的结构。
试题程序:
#include<string.h>
#include<stdi0.h>
#include<ctype.h>
void proc(char*p)
{
int i,t;char c[80];
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,strFS0];
int i=0:
printf("Input a string:");
c=getchar();
while(c!='#')
{
str[i]=c;i++;c=getchar();}
str[i]='\0 ';
proc(str);
puts(str);
}

进入在线模考

参考答案

暂无

答案解析

错误:c[t]="\0";
正确:c[t]='\0';
【解析】删除空格后的字符串放在数组e中,完成字符的复制后,要为新的字符串c添加结束符。因此,“c[t]="\0";”应改为“c[t]='\0 ';”。

你可能感兴趣的试题

找试题 · 找答案 · 找资料 在线模考