【问题标题】:merge two strings of different sizes in C在C中合并两个不同大小的字符串
【发布时间】:2017-10-05 22:34:09
【问题描述】:

如何将两个不同大小的字符串逐个字符地合并到A 的第n 个元素和B 的第m 个元素。当它们具有相同的大小时,我可以轻松地做到这一点(假设这里是n=m)。但不知道如何处理这个异常。

我的工作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 的第一个元素和Bm 的第一个元素。因此将以下输入传递给new_zip(char *A, char *B, int n, int m)A="rslxyzkw"; B="eutingxyz";n=3; m=6,我会得到"resulting"

【问题讨论】:

  • 您似乎没有考虑空终止符,还是 n 中包含空终止符?也就是n是代表数组大小还是字符串长度?

标签: c arrays string merge


【解决方案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;
}

【讨论】:

    【解决方案2】:

    您可以循环直到用完字符,而不是循环到某个长度。 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;
    }
    

    【讨论】:

    • 我真的很喜欢你的简洁明了。但是您的解决方案缺少nm 限制,对于SO 规则,我倾向于将@BLUEPIXY 解决方案标记为正确的解决方案。
    【解决方案3】:

    以下方法可行。

    #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;
    }
    

    【讨论】:

      【解决方案4】:

      以下代码将以下列方式工作:

      首先根据 m 和 n 的最小值从字符串 s1 和 s2 交替合并。 第二部分将负责从 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);
      }
      

      【讨论】:

        猜你喜欢
        • 2013-02-03
        • 1970-01-01
        • 2019-09-23
        • 2012-01-22
        • 2016-05-27
        • 1970-01-01
        • 2012-03-01
        • 2015-01-26
        相关资源
        最近更新 更多