llllmz

导航

1768. 交替合并字符串c

char * mergeAlternately(char * word1, char * word2){
    int n1=strlen(word1),n2=strlen(word2);
    char* temp=(char*)malloc(sizeof(char)*(n1+n2+1));
    int index1=0,index2=0,index=0,tag=0;
    while(index1<n1 && index2<n2){
        if(tag==0){
            temp[index++]=word1[index1++];
            tag=1;
        }else{
            temp[index++]=word2[index2++];
            tag=0;
        }
    }
    if(index1<n1){
        for(;index1<n1;index1++) temp[index++]=word1[index1];
    }else{
        for(;index2<n2;index2++) temp[index++]=word2[index2];
    }
    temp[n1+n2]=0;
    return temp;  
}

posted on 2024-03-15 16:52  神奇的萝卜丝  阅读(38)  评论(0)    收藏  举报