Concatenate Two Strings using Character Arrays

To concatenate two strings using character arrays in C, we can use the strcat() function from the string.h library or manually copy characters from one array to another. Since character arrays are fixed in size, we must ensure the destination array has enough space to hold both strings.

In this tutorial, we will cover multiple ways to concatenate strings using examples.


Examples of String Concatenation

1. Concatenating Two Strings Using strcat()

In this example, we will use the standard library function strcat() to concatenate two strings stored in character arrays.

main.c

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

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

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

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

Explanation:

  1. We declare two character arrays str1 and str2. str1 has extra space to accommodate the concatenated result.
  2. The strcat() function appends str2 to the end of str1, ensuring the null terminator is properly placed.
  3. We print the final concatenated string.

Output:

Concatenated String: Hello, World!

2. Concatenating Strings Manually Using a Loop

In this example, we will manually concatenate two character arrays by copying characters one by one using a while loop.

main.c

</>
Copy
#include <stdio.h>

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

    // Finding the length of str1
    i = 0;
    while (str1[i] != '\0') {
        i++;
    }

    // Copying str2 into str1
    j = 0;
    while (str2[j] != '\0') {
        str1[i] = str2[j];
        i++;
        j++;
    }
    str1[i] = '\0'; // Null terminate the final string

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

Explanation:

  1. We declare two character arrays, str1 with extra space and str2 containing the second string.
  2. We determine the length of str1 using a while loop to find the null terminator.
  3. We then copy characters from str2 into str1 starting at the position found in the first step.
  4. We append a null terminator at the end of str1 to ensure it remains a valid C string.
  5. The concatenated string is printed.

Output:

Concatenated String: Good Morning!

3. Concatenating Strings Using a Custom Function

In this example, we will create a custom function to concatenate two character arrays.

main.c

</>
Copy
#include <stdio.h>

void concatenate(char str1[], char str2[]) {
    int i = 0, j = 0;

    // Move to the end of str1
    while (str1[i] != '\0') {
        i++;
    }

    // Append str2 to str1
    while (str2[j] != '\0') {
        str1[i] = str2[j];
        i++;
        j++;
    }
    str1[i] = '\0'; // Null terminate the concatenated string
}

int main() {
    char str1[50] = "Apple ";
    char str2[] = "Banana!";

    // Call the function to concatenate strings
    concatenate(str1, str2);

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

Explanation:

  1. We define a function concatenate() that takes two character arrays as parameters.
  2. Inside the function, we find the length of str1 to determine where to append str2.
  3. We copy characters from str2 to str1 one by one using a loop.
  4. We ensure the final concatenated string is null-terminated.
  5. We call the function inside main() to concatenate and print the result.

Output:

Concatenated String: Apple Banana!

Conclusion

In this tutorial, we learned different ways to concatenate strings using character arrays in C:

  1. Using strcat(): Simplest way to concatenate strings.
  2. Using a loop: Manually copying characters to concatenate strings.
  3. Using a custom function: Reusable approach for string concatenation.