How to use the C language string concatenation function?

In the C language, the function used to concatenate strings is strcat(), and its prototype is as follows:

char* strcat(char* destination, const char* source);

The strcat() function appends the source string to the end of the destination string and returns a pointer to the destination string.

Here are the instructions for use:

  1. Firstly, ensure that the target string has enough space to accommodate the content of the source string.
  2. Call the strcat() function, passing the destination string as the first parameter and the source string as the second parameter.
  3. The function will append the content of the source string to the end of the target string.
  4. Finally, you can verify the results of the string concatenation by printing the target string.

Here is a simple example code:

#include <stdio.h>
#include <string.h>

int main() {
    char destination[20] = "Hello";
    char source[] = " World!";

    strcat(destination, source);

    printf("Concatenated string: %s\n", destination);

    return 0;
}

The output is: Combined string: Hello World!

bannerAds