2017-05-08 54 views
0

如何将不同大小的两个字符串合并到A的第n个元素和B的第m个元素。我可以很容易地做到这一点,当他们有相同的大小(假设在这里n=m)。但无法弄清楚如何处理这个异常。将两个不同大小的字符串合并为C

我的工作zip代码如下:

char * zip(char *A, char *B, int n) 
{ 
    char *C; 
    int i; 

    C = malloc((2*n) * sizeof *A); 

    for(i=0; i<n; i++) { 
      C[(2*i)]=A[i]; 
      C[(2*i)+1]=B[i]; 
    } 

    return C; 
} 

但是,相反只是int n通过,我想也通过int m其中n是要合并的An第一要素和的m第一要素B。因此将以下输入传递给new_zip(char *A, char *B, int n, int m)A="rslxyzkw"; B="eutingxyz";n=3; m=6,我会得到"resulting"

+0

您似乎没有考虑空终止符,或者包含在'n'中?也就是说,'n'代表数组大小还是字符串长度? – Lundin

回答

1

这样的:

#include <stdio.h> 
#include <stdlib.h> 
#include <assert.h> 

char *new_zip(char *A, char *B, int n, int m){ 
    assert(A != NULL && B != NULL && n >= 0 && m >= 0); 
    char *C = malloc(n + m + 1);//+1 for NUL 
    if(!C){ 
     perror("malloc:"); 
     return NULL; 
    } 
    int i = 0; 

    while(n + m > 0){ 
     if(n > 0 && *A){ 
      C[i++] = *A++; 
      --n; 
     } 
     if(m > 0 && *B){ 
      C[i++] = *B++; 
      --m; 
     } 
    } 
    C[i] = 0; 
    return C; 
} 

int main (void){ 
    char *result = new_zip("rslxyzkw", "eutingxyz", 3, 6); 
    printf("'%s'\n", result); 
    free(result); 
    return 0; 
} 
1

取代循环直到某个长度,您可以循环,直到用完字符。 C字符串在结尾处有一个空字符,因此在分配内存后,只要两者不是空字符,就可以循环。所有你需要做的只是添加非空字符到你的输出字符串。

#include <stdio.h> /* printf */ 
#include <stdlib.h> /* malloc, free */ 
#include <string.h> /* strlen */ 

char *zip(char *a, char *b) 
{ 
    char *c = malloc((strlen(a)+strlen(b)+1) * sizeof(char)), *p = c; 
    if(c) 
    { 
     while(*a || *b) /* while either string has characters */ 
     { 
     if(*a) *p++ = *a++; /* add a character from a if non-null */ 
     if(*b) *p++ = *b++; /* add a character from b if non-null */ 
     } 
     *p='\0'; /* finish the string with a null character */ 
    } 
    return c; 
} 

int main() 
{ 
    char *a = "This is a string"; 
    char *b = "This is another longer string"; 
    char *c = zip(a,b); 
    if(c) 
    { 
    printf("zip(%s,%s) = %s\n",a,b,c); 
    free(c); 
    } 
    return 0; 
} 
+0

我真的很享受你的简单和清晰。但是您的解决方案缺乏'n'和'm'限制,对于SO规则,我倾向于将@BLUEPIXY解决方案标记为正确的解决方案。 – Lin

0

下可以工作。

#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 

char* zip(char* A, char* B) { 
    char *C; 
    int k = 0; 

    C = (char*)malloc(strlen(A)+strlen(B)+1); 

    while (*A != '\0' || *B != '\0') { 
     if (*A != '\0') { 
      C[k++] = *A; 
      ++A; 
     } 
     if (*B != '\0') { 
      C[k++] = *B; 
      ++B; 
     } 
    } 
    C[k] = '\0'; 

    return C; 
} 



int main() { 
    char *A = "123456", *B = "abcd", *C; 

    C = zip(A, B); 

    puts(C); 

    return 0; 
} 
0

下面的代码将在以下的工作方式:

首先从串S1做备用合并和S2基于m和n的最小值。 第二部分将负责从s1或s2附加其余元素。

#include <stdio.h> 
#include <string.h> 
#include <stdlib.h> 

char* merge (char *s1, char *s2, int m, int n) 
{ 
    char *s = (char *) malloc(m + n + 1); 

    int min = (m < n)? m: n; 
    int i = 0, j = 0, k = 0; 

    int count = 0; 

    /* Alternate merge from s1 and s2 to s*/ 
    while (count < 2 * min) { 
     if (count % 2 == 0) { 
      s[k++] = s1[i++]; 
     } else { 
      s[k++] = s2[j++]; 
     } 
     count++; 
    } 

    /* Append the remaining elements from s1 or s2 to s*/ 
    if (m > min) { 
     for (count = 0; count < m - min; count++) { 
      s[k++] = s1[i++]; 
     } 
    } else if (n > min) { 
     for (count = 0; count < n - min; count++) { 
      s[k++] = s2[j++]; 
     } 
    } 
    s[k++] = '\0'; 
    return s; 
} 

int main() 
{ 
    char *s1 = "rslxyzkw"; 
    char *s2 = "eutingxyz"; 

    char *s = merge(s1, s2, 3, 6); 
    printf ("%s\n", s); 
}