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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18950495/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 07:31:43  来源:igfitidea点击:

merging two strings together in C, switching off characters

cmergestring-concatenation

提问by tbenz9

I am trying to merge two strings of variable length in C. The result should be 1st character from str1 then 1st character from str2 then 2nd character from str1, 2nd character from str2, etc. When it reaches the end of one string it should append the rest of the other string.

我试图在 C 中合并两个可变长度的字符串。结果应该是 str1 中的第一个字符,然后是 str2 中的第一个字符,然后是 str1 中的第二个字符,str2 中的第二个字符等。当它到达一个字符串的末尾时,它应该附加其他字符串的其余部分。

For Example:

例如:

str1 = "abcdefg";
str2 = "1234";

outputString = "a1b2c3d4efg";

I'm pretty new to C, my first idea was to convert both strings to arrays then try to iterate through the arrays but I thought there might be an easier method. Sample code would be appreciated.

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

UPDATE:I've tried to implement the answer below. My function looks like the following.

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

void strMerge(const char *s1, const char *s2, char *output, unsigned int ccDest)
{
    printf("string1 is %s\n", s1);
    printf("string2 is %s\n", s2);

    while (*s1 != '
$ 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]
' && *s2 != '
./strmerge abcdefg 12314135
string1 is abcdefg
string2 is 12314135
merged string is (null)
') { *output++ = *s1++; *output++ = *s2++; } while (*s1 != '
#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 %s\n", s1);
    printf("string2 is %s\n", s2);
    while (*s1 != '
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] !='
#include <stdio.h>
#include <string.h>

void interleave_strings(const char *s1, const char *s2, char *output)
{
    while (*s1 != '
$ ./interleave
abcdefgh
1234
In1: <<abcdefgh>>
In2: <<1234>>
Out: <<a1b2c3d4efgh>>
$
' && *s2 != '
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];
    }
}
') { *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); } }
' && 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; }
' && *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
') *output++ = *s1++; while (*s2 != '##代码##') *output++ = *s2++; *output = '##代码##'; printf("merged string is %s\n", *output); }

But I get a warning when compiling:

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

##代码##

And when I run it it doesnt work:

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

##代码##

Why does it think argument 2 is an int and how do I fix it to be a char? If I remove the "*" off output in the printf it doesn't give a compile error but the function still doesn't work.

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

采纳答案by Amarnath Krishnan

Your strMerge prints null because, you print the valueAt(*)output which was assigned null the previous step.

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

##代码##

回答by tbenz9

##代码##

回答by Jonathan Leffler

The code below ensures that the strings can't overflow by making the output string as long as the two input strings, and using fgets()to ensure that there is no overflow of the input strings. One alternative design would do dynamic memory allocation (malloc()et al), at the cost of the calling code having to free()the allocated space. Another design would pass the length of the output buffer to the function so that it could ensure no overflow occurs.

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

The test program doesn't emit prompts: it would not be hard to add a function to do so.

测试程序不会发出提示:添加一个函数来这样做并不难。

Code

代码

##代码##

Example output

示例输出

##代码##

回答by Tawnos

In C, both strings are already arrays that can be accessed by their pointers. You just need to create a new buffer that's large enough, then copy into it.

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

E.g. something like this:

例如这样的事情:

##代码##