char str[] ="hello\n""world\n";
char str2[]="csdn\n";
char str3[]="hello\n""world\n""csdn\n";
如何在str字符串中加入str2字符串,让他们变成str3字符串的格式,我尝试过很多合并字符串的代码但是最终形成的格式是"hello\nworld\ncsdn\n",我想要引号隔开的格式,麻烦各位了

c语言字符串如何添加或合并内容
- 写回答
- 好问题 0 提建议
- 关注问题
- 邀请回答
-
3条回答 默认 最新
- ShowMeAI 2023-01-10 08:42关注
在 C 语言中,如果你想要将两个字符串合并起来,你可以使用 strcat 函数。例如:
#include <stdio.h> #include <string.h> int main(void) { char str[] = "hello\nworld\n"; char str2[] = "csdn\n"; // 将 str2 拼接到 str 后面 strcat(str, str2); printf("%s", str); return 0; }
上面的代码会输出:
hello world csdn
如果你想要将字符串用引号括起来,你可以使用 printf 函数:
#include <stdio.h> #include <string.h> int main(void) { char str[] = "hello\nworld\n"; char str2[] = "csdn\n"; // 将 str2 拼接到 str 后面 strcat(str, str2); printf("\"%s\"", str); return 0; }
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报