问答题

改错题 下列给定程序中,函数fun()的功能是逐个比较a,b两个字符串对应位置中的字符,把ASCII值小或相等的字符依次存放到c数组中,形成一个新的字符串。 例如:a中的字符串为fshADfg,b中的字符串为sdAEdi,则c中的字符串应为fdAADf。 请改正程序中的错误,使它能得到正确结果。 注意:不要改动main函数,不得增行或删行,也不得更改程序的结构。 试题程序: #include #include void fun(char *p,char *q,char *c) { int k=0; while(*p||*q) /**********************found***********************/ { if (*p<=*q) c[k]=*q; else c[k]=*p; if(*p) p++; if(*q) q++ ; /**********************found***********************/ k++ } } main() { char a[10]="fshADfg",b[10]="sdAEdi",c[80]={’’\0’’}; fun(a,b,c); printf("The string a:"); puts(a); printf("The string b:"); puts(b); printf("The result :"); puts(c); }

【参考答案】

(1)错误: { if (*p<=*q) 正确:{ if (*p>=*q) (2)错误:k++ 正确:k++;
热门 试题

多项选择题
填空题请补充main函数,该函数的功能是:从键盘输入一个字符串并保存在字符str1中,把字符串str1中下标为偶数的字符保存在字符串str2中并输出。例如,当str1=“cdefghij”,则str2=“cegi”。注意:部分源程序给出如下。请勿改动主函数main和其他函数中的任何内容,仅在函数fun()的横线上填入所编写的若干表达式或语句。试题程序:#include#include#define LEN 80main(){ char str1[LEN],str2[LEN]; char *p1=str1,*p2=str2; int i=0,j=0; clrscr(); printf( Enter the string: n ); scanf(【1】); printf( ***the origial string*** n ); while(*(p1+j)) { printf( 【2】 ,*(p1+j)); j++; } for(i=0;i<j;i+=2) *p2++=*(str1+i); *p2=’’ 0’’; printf( nThe new string is:%s n ,【3】);}