字符串合并出错
#include <stdio.h>#include <stdlib.h>#include <string.h>void Merge(int ch1[], int ch2[], int ch3[]){ int k; int i; int j; int len1 = strlen(ch1); int len2 = strlen(ch2); while (i<len1 && j<len2) { if (ch1[i]<ch2[j]) ch3[k++] = ch1[i++]; else if(ch1[i] > ch2[j]) ch3[k++] = ch2[j++]; } if (i<len1) ch3[k++]= ch1[i++]; if (j < len1) ch3[k++] = ch2[j++]; for (k; k>=0; k--) printf("%d ", ch3[k]);}int main(){ int ch1[] = {2,4,6,8}; int ch2[] = {1,3,5,7}; int ch3[10]; int k; Merge(ch1, ch2, ch3); for (k =0;k<10;k++) printf("%d ",ch3[k]); return 0;}
void Merge(int ch1[], int ch2[], int ch3[]){ int k=0; int i=0; int j=0; int len1 = strlen(ch1); int len2 = strlen(ch2); while (i<len1 && j<len2) { if (ch1[i]<ch2[j]) ch3[k++] = ch1[i++]; else if(ch1[i] > ch2[j]) ch3[k++] = ch2[j++]; } while(i<len1) ch3[k++]= ch1[i++]; while (j < len1) ch3[k++] = ch2[j++]; for (k; k>=0; k--) printf("%d ", ch3[k]);}
[解决办法]
i,j,k都不给赋初值。。。注意