您可以使用循环来实现字符串连接,以下是一个示例程序:

#include <stdio.h>

void stringConcat(char dest[], const char src[]) {
    int i = 0;
    int j = 0;
    
    // 寻找目标字符串的末尾位置
    while (dest[i] != '\0') {
        i++;
    }
    
    // 将源字符串追加到目标字符串末尾
    while (src[j] != '\0') {
        dest[i] = src[j];
        i++;
        j++;
    }
    
    // 添加目标字符串的终止符
    dest[i] = '\0';
}

int main() {
    char str1[100] = "Hello, ";
    char str2[] = "world!";
    
    stringConcat(str1, str2);
    
    printf("Concatenated string: %s\n", str1);
    
    return 0;
}

在上面的示例程序中,我们定义了一个 stringConcat 函数来实现字符串连接。该函数接受两个参数,分别是目标字符串 dest 和源字符串 src。使用两个循环,首先找到目标字符串的末尾位置,然后将源字符串的每个字符追加到目标字符串的末尾。最后,我们添加目标字符串的终止符 \0 来表示字符串的结束。

main 函数中,我们声明了两个字符串 str1str2,并将初始值赋给 str1。然后,我们调用 stringConcat 函数将 str2 连接到 str1 的末尾。最后,我们使用 printf 函数输出连接后的结果。

运行上面的程序,输出将会是:

Concatenated string: Hello, world!
``
编写一个程序将两个字符串连接起来不要用 strcat 或 strncat 函数

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

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