Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
字符数组C中的字符插入_C_Arrays_String_Character_Concatenation - Fatal编程技术网

字符数组C中的字符插入

字符数组C中的字符插入,c,arrays,string,character,concatenation,C,Arrays,String,Character,Concatenation,我试图在字符数组中插入一个特殊字符“-”,并希望将其保存在另一个字符串中。要插入的字符使用循环完成 输入: actgagc atgac 输出 actgagc a-tga-c 在第二个字符数组中,插入-以替换缺少的字符 该程序在JAVA中使用简单的字符串连接函数完成,但无法为C语言插入字符 Java代码是: while (k > 0 && l > 0) { if (condition 1) { r_string1

我试图在字符数组中插入一个特殊字符“-”,并希望将其保存在另一个字符串中。要插入的字符使用循环完成

输入:

actgagc

atgac

输出

actgagc

a-tga-c

在第二个字符数组中,插入-以替换缺少的字符

该程序在JAVA中使用简单的字符串连接函数完成,但无法为C语言插入字符

Java代码是:

while (k > 0 && l > 0) {
            if (condition 1) {
                r_string1 += string_1[k - 1];
                r_string2 += string_2[l - 1];
                k--;
                l--;
            } else if (condition) {
                r_string1 += "-";
                r_string2 += string_2[l - 1];
                l--;
            } else {
                r_string1 += sring_1[k - 1];
                r_string2 += "-";
                k--;
            }
        }

在C语言中,我尝试在字符数组中插入字符。尝试使用strcat、memmov函数,但未取得任何结果。

您需要为新字符串分配空间,因此不要忘记释放它

char * f(char *s1, char * s2)
{
    char *s3 = malloc(strlen(s2)+1);
    char *ret = s3;

    while (*s2)
    {
        if (*s1++ == *s2)
            *s3 = *s2++;
        else
            *s3 = '-';
        s3++;
    }
    *s3 = '\0';
    return ret;
}

因此,不是人力运输服务。向我们展示您迄今为止在C语言中尝试的内容。现在,您已经有了一些Java伪代码,但是您没有向我们展示您在C中尝试获取相同内容的内容。请阅读“帮助”部分,了解什么是“好问题”