Home » C Program » C Program to Concatenate two Strings

C Program to Concatenate two Strings

Last updated:

Support this post with a reaction:

4,953 views

String concatenation in C is the process of combining two or more strings into a single string. In C programming, strings are character arrays terminated by a null character ('\0'). The strcat() function, available in the string.h header file, is commonly used to concatenate one string to the end of another string.

For example, combining "Hello" and "World" produces "HelloWorld". In this tutorial, you will learn how to concatenate two strings in C using the strcat() function and without using built-in string functions, along with example programs and their outputs.

What Is String Concatenation in C?

String concatenation in C means combining two or more strings to form a single string.

C does not provide a dedicated string data type. Instead, strings are represented using character arrays terminated by a null character ('\0').

During concatenation, the characters of the second string are appended to the end of the first string, and the resulting string is terminated with a null character.

Syntax:

strcat(destination, source);

Here:

  • destination: The string at the end of which another string is appended.
  • source: The string that needs to be appended.
  • strcat(): The standard library function used to concatenate strings.

Example of String Concatenation

First String  = "Hello"
Second String = "World"

Concatenated String = "HelloWorld"

Different Ways to Concatenate Two Strings

There are two commonly used methods:

MethodDescription
Using strcat()Uses the built-in string concatenation function.
Without using strcat()Uses a loop to copy characters manually.

Let’s understand both methods with complete C programs.

C Program to Concatenate Two Strings

1. Without Using strcat()

We can concatenate two strings without using the strcat() function by manually copying the characters of the second string to the end of the first string.

C Program to concatenate string without using strcat()

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

int main()
{
    char str1[200], str2[100];
    int i, j;

    printf("Enter first string: ");
    fgets(str1, sizeof(str1), stdin);

    printf("Enter second string: ");
    fgets(str2, sizeof(str2), stdin);

    // Remove newline characters
    str1[strcspn(str1, "\n")] = '\0';
    str2[strcspn(str2, "\n")] = '\0';

    // Find the end of the first string
    i = strlen(str1);

    // Copy second string to the end of first string
    for (j = 0; str2[j] != '\0'; j++)
    {
        str1[i + j] = str2[j];
    }

    // Add null character
    str1[i + j] = '\0';

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

    return 0;
}

Output:

Enter first string: Hello
Enter second string: World
Concatenated string: HelloWorld

Explanation of Above Program

Let’s understand the program step by step:

  1. Declare two character arrays: We declare str1 and str2 to store the two input strings. The first array has sufficient space to store the combined result.
  2. Read the input strings: We use fgets() to read both strings. Unlike scanf("%s"), it can also accept spaces within strings.
  3. Remove newline characters: The strcspn() function helps remove the newline character added by fgets().
  4. Find the length of the first string: The strlen() function calculates the number of characters in the first string, excluding the null character.
  5. Copy the second string: A for loop copies each character of str2 to the end of str1.
  6. Add the null character: We add '\0' at the end to ensure that the resulting character array is a valid C string.
  7. Display the result: Finally, we print the concatenated string.

Dry Run of the Program

Consider the following input:

str1 = "Hello"
str2 = "World"

The length of the first string is 5.

Initially:

str1 = H e l l o \0
str2 = W o r l d \0

The loop copies the characters of str2 into the available positions of str1.

IterationCharacter copiedPositionUpdated string
1W5HelloW
2o6HelloWo
3r7HelloWor
4l8HelloWorl
5d9HelloWorld

After the loop, the null character is inserted at position 10.

Final Result:

HelloWorld

2. Using strcat()

The strcat() function is a predefined string-handling function available in the string.h header file.

It appends the contents of the source string to the destination string.

Syntax

strcat(destination, source);

Example of strcat()

char str1[20] = "Hello";
char str2[] = "World";

strcat(str1, str2);

printf("%s", str1);

Output:

HelloWorld

Note: In C, You can declare a character array without specifying its size when you initialize it with a string literal like we have done for str2[].

C Program to concatenate two strings using strcat()

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

int main()
{
    char str1[200], str2[100];

    printf("Enter first string: ");
    fgets(str1, sizeof(str1), stdin);

    printf("Enter second string: ");
    fgets(str2, sizeof(str2), stdin);

    // Remove newline characters
    str1[strcspn(str1, "\n")] = '\0';
    str2[strcspn(str2, "\n")] = '\0';

    // Concatenate two strings
    strcat(str1, str2);

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

    return 0;
}

Output:

Enter first string: Hello
Enter second string: World
Concatenated string: HelloWorld

Explanation of Above Program

  1. Declare two character arrays to store the input strings.
  2. Read both strings using fgets().
  3. Remove the newline characters from both strings.
  4. Pass the two strings to the strcat() function.
  5. The function appends the second string to the end of the first string.
  6. Display the resulting string.