How can two strings be concatenated in the C language?
In C language, we can use the strcat() function to concatenate two strings. The prototype of the strcat() function is as follows:
char *strcat(char *dest, const char *src);
In this case, ‘dest’ is the target string and ‘src’ is the source string to be concatenated. Using this function allows you to append the content of the ‘src’ string to the end of the ‘dest’ string.
Here is an example code:
#include <stdio.h>
#include <string.h>
int main() {
char str1[50] = "Hello";
char str2[50] = " World!";
strcat(str1, str2);
printf("拼接后的字符串:%s\n", str1);
return 0;
}
Output of the program:
拼接后的字符串:Hello World!
In the example, two string arrays, str1 and str2, are first defined and initialized as “Hello” and ” World!”. Then, the contents of str2 are concatenated to the end of str1 using the strcat() function, and the concatenated result is outputted.