所属年份:2010.9;2011.3;2011.9;
给定程序中函数fun的功能是:首先把b所指字符串中的字符按逆序存放,然后将a所指字符串中的字符和b所指字符串中的字符,按排列的顺序交叉合并到c所指数组中,过长的剩余字符接在c所指的数组的尾部。例如,当a所指字符串中的内容为”abcdefg”,b所指字符串中的内容为”1234″时,c所指数组中的内容应为”a4b3c2d1efg”;而当a所指字符串中的内容为”1234″,b所指字符串的内容为”abcdefg”时,c所指数组中的内容应该为”1g2f3e4dcba”。
请改正程序中的错误,使它能得出正确的结果。
注意:部分源程序在文件MODI1.C中,不要改动main函数,不得增行或删行,也不得更改程序的结构!
#include <stdio.h> #include <string.h> void fun( char *a, char *b, char *c ) { int i , j; char ch; i = 0; j = strlen(b)-1; /************found************/ while ( i > j ) { ch = b[i]; b[i] = b[j]; b[j] = ch; i++; j--; } while ( *a || *b ) { /************found************/ If ( *a ) { *c = *a; c++; a++; } if ( *b ) { *c = *b; c++; b++; } } *c = 0; } main() { char s1[100],s2[100],t[200]; printf("\nEnter s1 string : "); scanf("%s",s1); printf("\nEnter s2 string : "); scanf("%s",s2); fun( s1, s2, t ); printf("\nThe result is : %s\n", t ); }
【参考答案】
(1)while ( i < j ) (2)if ( *a )
【解题思路】
(1)由i和j定义的初始值可知,此处应该判断i是否小于j,所以应改为while(i