在C语言中,字符串拼接有多种方法,其中一些常见的方法包括:
使用strcat()函数:该函数可以将一个字符串追加到另一个字符串的末尾。#include <string.h>char str1[20] = "Hello";char str2[20] = "World";strcat(str1, str2);printf("%s\n", str1); // 输出结果为"HelloWorld"使用sprintf()函数:该函数可以将格式化的数据写入一个字符串中。char str[20];int num = 123;sprintf(str, "The number is: %d", num);printf("%s\n", str); // 输出结果为"The number is: 123"使用strncat()函数:该函数可以将一个字符串的指定长度追加到另一个字符串的末尾。#include <string.h>char str1[20] = "Hello";char str2[20] = "World";strncat(str1, str2, 3);printf("%s\n", str1); // 输出结果为"HelloWor"使用strcpy()函数和strcat()函数结合:首先使用strcpy()函数将一个字符串复制到另一个字符串,然后使用strcat()函数追加另一个字符串。#include <string.h>char str1[20] = "Hello";char str2[20] = "World";strcpy(str1, "Hello");strcat(str1, str2);printf("%s\n", str1); // 输出结果为"HelloWorld"这些都是常见的字符串拼接方法,可以根据具体的需求选择合适的方法进行字符串拼接。




