c语言怎么合并两个字符串

   2025-02-04 2530
核心提示:在C语言中,可以使用库函数strcat()来合并两个字符串。#include stdio.h#include string.hint main() {char str1[50] = Hello ;c

在C语言中,可以使用库函数strcat()来合并两个字符串。

#include <stdio.h>#include <string.h>int main() {    char str1[50] = "Hello ";    char str2[] = "world!";        strcat(str1, str2);        printf("合并后的字符串:%s\n", str1);        return 0;}

输出结果为:

合并后的字符串:Hello world!

这里的str1是要合并的目标字符串,str2是要追加到str1后面的字符串。strcat()函数会将str2追加到str1的末尾,并在合并后的字符串的末尾添加一个空字符\0

 
 
更多>同类维修知识
推荐图文
推荐维修知识
点击排行
网站首页  |  关于我们  |  联系方式  |  用户协议  |  隐私政策  |  网站留言