C语言字符串拼接:使用 strcat() 函数
在 C 语言中,可以使用 strcat() 函数来拼接两个字符串。strcat() 函数将第二个字符串追加到第一个字符串的末尾,并将结果存储在第一个字符串中。
以下是一个示例代码:
#include <stdio.h>
#include <string.h>
int main() {
char str1[20] = 'hello';
char str2[20] = 'world';
strcat(str1, str2);
printf('拼接后的字符串为:%s', str1);
return 0;
}
输出结果为:
拼接后的字符串为:helloworld
在上面的代码中,我们首先定义了两个字符串 str1 和 str2,然后使用 strcat() 函数将 str2 拼接到 str1 的末尾,并将结果存储在 str1 中。最后使用 printf() 函数输出拼接后的字符串。
原文地址: https://www.cveoy.top/t/topic/m6JJ 著作权归作者所有。请勿转载和采集!