可以使用循环遍历两个字符串,将第二个字符串的每个字符逐一添加到第一个字符串的末尾。具体实现如下:

#include <stdio.h>

void string_concat(char str1[], char str2[]) {
    int i, j;
    i = j = 0;
    while (str1[i] != '\0') {
        i++;
    }
    while (str2[j] != '\0') {
        str1[i] = str2[j];
        i++;
        j++;
    }
    str1[i] = '\0';
}

int main() {
    char str1[50] = "Hello";
    char str2[] = " World!";
    string_concat(str1, str2);
    printf("%s\n", str1); // 输出:Hello World!
    return 0;
}

string_concat 函数中,我们使用两个变量 ij 循环遍历两个字符串。首先找到第一个字符串的末尾,即以 \0 结尾的位置,然后将第二个字符串的每个字符逐一添加到第一个字符串的末尾。最后,需要在第一个字符串的结尾添加一个 \0,以表示字符串的结束。

在主函数中,我们定义了两个字符串 str1str2,并将 str2 合并到 str1 中。最后,我们使用 printf 函数输出合并后的字符串。

在不知道strcat函数的情况下怎么用c语言合并两个字符串的程序

原文地址: https://www.cveoy.top/t/topic/bkiL 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录