C Program to Concatenate Two Strings

Updated on December 11, 2024
Concatenate two strings header image

Introduction

In the world of C programming, string manipulation is a fundamental skill that every developer needs to master. Among these string operations, concatenation—combining two strings into one—is particularly useful for creating custom messages, constructing queries, and handling user input.

In this article, you will learn how to concatenate two strings in C using different methods. Explore practical examples that enhance your understanding of handling character arrays and pointers in real-world programming scenarios.

Basic String Concatenation using strcat()

Understanding strcat()

  1. strcat() is a standard library function that appends the content of one string to another.
  2. Ensure the destination string has enough space to hold the combined result.

Example: Concatenating Two Strings

  1. Include the necessary header file.

  2. Declare and initialize the strings.

  3. Perform the concatenation using strcat().

    c
    #include <stdio.h>
    #include <string.h>
    
    int main() {
        char dest[20] = "Hello, ";
        char src[] = "World!";
    
        strcat(dest, src);
        printf("Concatenated String: %s\n", dest);
        return 0;
    }
    

    In this example, dest initially contains "Hello, " which is then concatenated with "World!". The output of the program will be "Hello, World!".

Manual Concatenation Using Loops

Steps for Manual Concatenation

  1. Initialize and declare the strings.
  2. Use a loop to append characters manually.

Example: Manual Concatenation

  1. This method gives fine control over string manipulation but requires more lines of code.

    c
    #include <stdio.h>
    
    int main() {
        char dest[20] = "Hello, ";
        char src[] = "World!";
        int i, j;
    
        // Find the end of the first string
        for (i = 0; dest[i] != '\0'; i++);
    
        // Append second string
        for (j = 0; src[j] != '\0'; j++, i++) {
            dest[i] = src[j];
        }
    
        dest[i] = '\0'; // Terminate with a null character
        printf("Concatenated String: %s\n", dest);
        return 0;
    }
    

    This code manually loops through each string to concatenate them. It first determines the end of dest and then continues copying characters from src.

Using Pointers for String Concatenation

Steps for Pointer-Based Concatenation

  1. Pointers can be used for efficient manipulation of strings.
  2. Utilize pointer arithmetic to traverse and modify strings.

Example: Concatenation with Pointers

  1. This example employs pointer arithmetic for a more efficient approach.

    c
    #include <stdio.h>
    
    void strcat_pointer(char *dest, char *src) {
        while (*dest) dest++; // Move to the end of dest
        while (*src) *dest++ = *src++; // Copy src to dest
        *dest = '\0';
    }
    
    int main() {
        char dest[20] = "Hello, ";
        char src[] = "World!";
    
        strcat_pointer(dest, src);
        printf("Concatenated String: %s\n", dest);
        return 0;
    }
    

    By using pointers, the function strcat_pointer moves to the end of dest and then copies src over. This eliminates the need for managing indices manually.

Conclusion

Concatenating strings in C can be accomplished through several methods, each suitable for different situations. The strcat() function is simple and straightforward for most cases. However, for scenarios requiring granular control or efficiency, manual loops and pointers offer powerful alternatives. Employ these techniques in your projects to handle string concatenation with ease and flexibility.