首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

字符串合并出错,该怎么解决

2012-03-11 
字符串合并出错C/C++ code#include stdio.h#include stdlib.h#include string.hvoid Merge(int ch1[

字符串合并出错

C/C++ code
#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;}


[解决办法]
C/C++ code
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都不给赋初值。。。注意

热点排行