下列给定程序中,函数fun()的功能是:将字符串p中所有字符复制到字符串b中,要求每复制3个字符之后插入一个空格。例如,在调用fun()函数之前给字符串a输入ABCDEFGHIJK,调用函数之后,字符串b中的内容则为ABC DEF GHI JK。
请改正程序中的错误,使它能得出正确的结果。
注意:不要改动main函数,不得增行或删行,也不得更改程序的结构。
试题程序:
#include <stdio. h>
void fun (char *p, char *b)
int i, k=0;
while (*p)
/*************found***************/
i=l;
/*************found***************/
while (i<3|| *P)
b[k]=*p;
k++; p++; i++;
if (*p)
/*************found***************/
b[k]= ’ ’;
b[k]= ’\0’;
main ( )
char a[80],b[80];
printf ("Enter a string: "); gets (a);
printf ("The original string: ");
puts (a);
fun (a,b);
printf("\nThe string after insert
space: "); puts(b); printf("\n\n ");
参考答案:错误:i=1; 正确:i=0;
(2)错误:while(i<3||*p) 正确:while(i<3&&*p)
(3)错误:b[k]=’’; 正确:b[k++]=’’;
解析: 该题考查字符串复制程序的构造方法,题中插入空格字符的定义条件是while(i<3||*p),当然要同时满足i<3和*p这两个条件后才能执行{b(k++]=’’;}这个语句,该语句的含义是在输出结果中置入空格字符。