Concatenate Two Strings in C

In C, we can concatenate (combine) two strings using various methods such as the strcat() function from the string.h library, manually appending characters using loops, or using pointer arithmetic.

In this tutorial, we will explore different ways to concatenate two strings with detailed explanations and examples.


Examples of String Concatenation in C

1. Using strcat() Function to Concatenate Strings

In this example, we will use the built-in strcat() function from the string.h library to concatenate two strings.

main.c

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

int main() {
    char str1[100] = "Hello, ";
    char str2[] = "World!";

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

    printf("Concatenated String: %s\n", str1);
    return 0;
}

Explanation:

  1. We declare a character array str1 with a sufficient buffer size (100 characters) to store the final concatenated string.
  2. We declare another character array str2 containing the string to be appended.
  3. The strcat() function appends str2 to the end of str1.
  4. Finally, we print the concatenated string.

Output:

Concatenated String: Hello, World!

2. Concatenating Strings Using sprintf()

In this example, we will use the sprintf() function to concatenate two strings into a buffer.

main.c

</>
Copy
#include <stdio.h>

int main() {
    char str1[] = "Hello, ";
    char str2[] = "Arjun!";
    char result[100];

    // Concatenating using sprintf
    sprintf(result, "%s%s", str1, str2);

    printf("Concatenated String: %s\n", result);
    return 0;
}

Explanation:

  1. We declare two character arrays, str1 and str2, containing the strings to concatenate.
  2. A third character array, result, is created to store the concatenated output.
  3. The sprintf() function is used to format and concatenate str1 and str2 into result.
  4. Finally, we print the concatenated string.

Output:

Concatenated String: Hello, Arjun!

3. Concatenating Strings Manually Using a Loop

In this example, we will manually append characters from the second string to the first string using a loop.

main.c

</>
Copy
#include <stdio.h>

int main() {
    char str1[100] = "Good ";
    char str2[] = "Morning!";
    int i, j;

    // Find length of str1
    for (i = 0; str1[i] != '\0'; i++);

    // Append characters of str2 to str1
    for (j = 0; str2[j] != '\0'; j++, i++) {
        str1[i] = str2[j];
    }

    // Null terminate str1
    str1[i] = '\0';

    printf("Concatenated String: %s\n", str1);
    return 0;
}

Explanation:

  1. We declare str1 with enough buffer space and initialize it with “Good “.
  2. We declare str2 as “Morning!” to be appended.
  3. A loop finds the length of str1 to determine the starting position for appending.
  4. Another loop appends characters from str2 to str1 one by one.
  5. We manually insert the null character '\0' to terminate the string.

Output:

Concatenated String: Good Morning!

Conclusion

In this tutorial, we explored multiple ways to concatenate two strings in C:

  1. Using strcat(): A built-in function for string concatenation.
  2. Using sprintf(): A formatted way to combine strings.
  3. Manual Concatenation: Using loops to append characters.

Each method has its own use case. The strcat() function is easy to use, while sprintf() is flexible for formatted output. Manually appending characters gives full control over string manipulation.