以下是使用C语言拼接两个字符串的示例代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
    char str1[50], str2[50];
    printf("Enter the first string: ");
    fgets(str1, 50, stdin);
    printf("Enter the second string: ");
    fgets(str2, 50, stdin);

    // Remove newline character from the end of the first string
    str1[strcspn(str1, "\n")] = 0;

    // Calculate the length of the first string
    int len1 = strlen(str1);

    // Concatenate the second string to the end of the first string
    strcat(str1, str2);

    printf("The concatenated string is: %s\n", str1);

    return 0;
}

以上代码首先从键盘输入两个字符串,然后使用strcspn()函数去除第一个字符串末尾的换行符,接着使用strlen()函数计算第一个字符串的长度,最后使用strcat()函数将第二个字符串拼接到第一个字符串的末尾,并输出拼接后的字符串。

用c语言拼接两个字符串字符串由键盘输入

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

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