在 C 中将两个字符串合并在一起,关闭字符



我正在尝试合并 C 中两个可变长度的字符串。 结果应该是 str1 中的第 1 个字符,然后是 str2 中的第 1 个字符,然后是 str1 中的第 2 个字符,str2 中的第 2 个字符,依此类推。 当它到达一个字符串的末尾时,它应该附加另一个字符串的其余部分。

例如:

str1 = "abcdefg";
str2 = "1234";
outputString = "a1b2c3d4efg";

我对 C 很陌生,我的第一个想法是将两个字符串转换为数组,然后尝试遍历数组,但我认为可能有更简单的方法。 示例代码将不胜感激。

更新:我试图实现下面的答案。 我的函数如下所示。

void strMerge(const char *s1, const char *s2, char *output, unsigned int ccDest)
{
    printf("string1 is %sn", s1);
    printf("string2 is %sn", s2);
    while (*s1 != '' && *s2 != '')
    {
        *output++ = *s1++;
        *output++ = *s2++;
    }
    while (*s1 != '')
        *output++ = *s1++;
    while (*s2 != '')
        *output++ = *s2++;
    *output = '';
    printf("merged string is %sn", *output);
}

但是我在编译时收到警告:

$ gcc -g -std=c99 strmerge.c -o strmerge
strmerge2.c: In function ‘strMerge’:
strmerge2.c:41:5: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘int’ [-Wformat]

当我运行它时,它不起作用:

./strmerge abcdefg 12314135
string1 is abcdefg
string2 is 12314135
merged string is (null)

为什么它认为参数 2 是一个 int,我如何将其修复为字符? 如果我删除 printf 中的"*"关闭输出,它不会给出编译错误,但该函数仍然不起作用。

下面的代码通过使输出字符串与两个输入字符串一样长,并使用fgets()来确保输入字符串没有溢出,从而确保字符串不会溢出。另一种设计是动态内存分配(malloc()等人),代价是调用代码必须free()分配的空间。 另一种设计会将输出缓冲区的长度传递给函数,以便它可以确保不会发生溢出。

测试程序不会发出提示:添加函数来执行此操作并不难。

法典

#include <stdio.h>
#include <string.h>
void interleave_strings(const char *s1, const char *s2, char *output)
{
    while (*s1 != '' && *s2 != '')
    {
        *output++ = *s1++;
        *output++ = *s2++;
    }
    while (*s1 != '')
        *output++ = *s1++;
    while (*s2 != '')
        *output++ = *s2++;
    *output = '';
}
int main(void)
{
    char line1[100];
    char line2[100];
    char output[200];
    if (fgets(line1, sizeof(line1), stdin) != 0 &&
        fgets(line2, sizeof(line2), stdin) != 0)
    {
        char *end1 = line1 + strlen(line1) - 1;
        char *end2 = line2 + strlen(line2) - 1;
        if (*end1 == 'n')
            *end1 = '';
        if (*end2 == 'n')
            *end2 = '';
        interleave_strings(line1, line2, output);
        printf("In1: <<%s>>n", line1);
        printf("In2: <<%s>>n", line2);
        printf("Out: <<%s>>n", output);
    }
}

示例输出

$ ./interleave
abcdefgh
1234
In1: <<abcdefgh>>
In2: <<1234>>
Out: <<a1b2c3d4efgh>>
$
char* getMerged(const char* str1, const char* str2) {
  char* str = malloc(strlen(str1)+strlen(str2)+1);
  int k=0,i;
  for(i=0;str1[i] !='' && str2[i] !='';i++) {
         str[k++] = str1[i];
         str[k++] = str2[i];
  }
  str[k]='';
  if (str1[i] != '') {
        strcpy(&str[k], &str1[i]); 
  } else if (str2[i] != '') {
        strcpy(&str[k], &str2[i]);
  }
 return str; 
}

您的 strMerge 打印 null 是因为您打印了在上一步中被分配为 null 的 valueAt(*) 输出。

#include<stdio.h>
#include<string.h>
//strMerge merges two string as per user requirement
void strMerge(const char *s1, const char *s2, char *output)
{
    printf("string1 is %sn", s1);
    printf("string2 is %sn", s2);
    while (*s1 != '' && *s2 != '')
    {
        *output++= *s1++;
    *output++ = *s2++;
    }
    while (*s1 != '')
        *output++=*s1++;
    while (*s2 != '')
        *output++ = *s2++;
    *output='';
}
int main()
{
    char *str1="abcdefg"; 
    char *str2="1234";
    char *output=malloc(strlen(str1)+strlen(str2)+1); //allocate memory 7+4+1 = 12 in this case
    strMerge(str1,str2,output); 
    printf("%s",output);
    return 0;
}
OUTPUT:
   string1 is abcdefg
   string2 is 1234
   a1b2c3d4efg

在 C 中,这两个字符串都已经是可以通过其指针访问的数组。您只需要创建一个足够大的新缓冲区,然后复制到其中。

例如,像这样:

int str1Length = strlen(str1);
int str2Length = strlen(str2);
char* output = (char*) malloc(str1Length + str2Length + 1);
int j = 0;
for (int i = 0; i < str1Length; i++)
{
    output[j++] = str1[i];
    if (str2Length < i)
        output[j++] = str2[i];
}
if (str2Length > str1Length)
{
    for (int i = str2Length - str1Length; i < str2Length; i++)
    {
        output[j++] = str2[i];
    }
}

最新更新