Home » Programming Languages » C Programs » How to Concatenate Two Strings Using strcat() in C: A Beginner’s Guide

How to Concatenate Two Strings Using strcat() in C: A Beginner’s Guide

String manipulation is a crucial part of programming, and one common task is concatenating two strings. In the C programming language, the standard library provides a function called strcat() for this purpose. This function allows you to append one string to another, creating a single combined string.

In this blog post, we’ll walk you through how to concatenate two strings using the strcat() function in C. We’ll provide simple, step-by-step examples to help you understand the concept and how to apply it in your programs.

What is strcat() in C?

The strcat() function is defined in the string.h library and is used to append one string to the end of another. The syntax of strcat() is as follows :

char *strcat(char *destination, const char *source);
  • destination: This is the string to which the content of the source string will be appended.
  • source: This is the string that will be added to the end of the destination string.

The strcat() function modifies the destination string by appending the source string and returns a pointer to the destination.

Example of Concatenating Two Strings Using strcat()

Here is a simple example to illustrate how the strcat() function works:

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

int main() {
    char str1[100] = "Hello, ";  // The destination string (with enough space for concatenation)
    char str2[] = "World!";      // The source string

    // Concatenating str2 to str1
    strcat(str1, str2);

    // Printing the result
    printf("Concatenated String: %s\n", str1);

    return 0;
}

Explanation of the Code:

  1. Including Libraries: We include stdio.h for input/output functions and string.h for string manipulation functions like strcat().
  2. Defining Strings:
  • str1 is the destination string. It is initialized with the value “Hello, ” and allocated enough space to hold the concatenated result.
  • str2 is the source string that contains “World!”.
  1. Using strcat(): The strcat() function appends the content of str2 to the end of str1.
  2. Printing the Concatenated String: After concatenation, we print the result using printf().

Output:

Concatenated String: Hello, World!

Key Points to Remember

  1. Sufficient Space: The destination string (str1) must have enough space to hold the result after concatenation. If it does not, the program may result in a buffer overflow, which can cause unexpected behavior or a crash. Always ensure the size of the destination array is large enough to accommodate both strings.
  2. Null-Terminator: The strcat() function automatically adds a null terminator (\0) to the end of the concatenated string, so you don’t need to worry about manually adding it.
  3. Order of Concatenation: The strcat() function appends the source string to the destination string, not the other way around. Make sure the destination string is the one you want to hold the combined result.

Concatenating Multiple Strings

You can also concatenate more than two strings using strcat(). For example:

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

int main() {
    char str1[100] = "Good ";   // Destination string
    char str2[] = "Morning, ";  // Source string 1
    char str3[] = "World!";     // Source string 2

    // Concatenating multiple strings
    strcat(str1, str2);
    strcat(str1, str3);

    // Printing the final concatenated string
    printf("Concatenated String: %s\n", str1);

    return 0;
}

Output:

Concatenated String: Good Morning, World!

Conclusion

The strcat() function is a powerful and simple tool for concatenating strings in C. It is important to ensure that your destination string has enough space to hold the final result to avoid memory issues. By mastering the use of strcat(), you can easily manipulate strings and perform tasks like building dynamic text in your programs.

Leave a Comment