C program to concatenate two strings using pointers

Category: C Program
Tags: #cprogram#pointers#string

Write a C program to concatenate two strings efficiently. Learn two methods for merging strings: in-place concatenation or generating a new merged string.

In C programming, string manipulation is a fundamental operation often required in various applications. One common operation is concatenating two strings, which involves combining the characters of one string with those of another. While C provides built-in functions like strcat() for string concatenation, implementing this operation using pointers offers insights into memory management and pointer arithmetic. In this article, we'll explore how to concatenate two strings using pointers in C.

Concatenating Strings using Pointers

To concatenate two strings using pointers in C, we'll iterate through the characters of the first string until we reach the null terminator (\0). Then, we'll copy the characters of the second string to the end of the first string. Here's a step-by-step guide to concatenating strings using pointers:

  1. Declare Pointers: Define pointers to the first and second strings.

    void stringConcat(char *dest, const char *src) {
        // Move the pointer to the end of the destination string
        while (*dest != '\0') {
            dest++;
        }
    
        // Copy characters from src to dest
        while (*src != '\0') {
            *dest = *src;
            src++;
            dest++;
        }
    
        // Add null terminator to the concatenated string
        *dest = '\0';
    }
    
  2. Move Pointer to End: Iterate through the characters of the first string using a pointer until we reach the null terminator.

    // Move the pointer to the end of the destination string
    while (*dest != '\0') {
        dest++;
    }
    
  3. Copy Characters: Copy characters from the second string to the end of the first string.

    // Copy characters from src to dest
    while (*src != '\0') {
        *dest = *src;
        src++;
        dest++;
    }
    
  4. Add Null Terminator: After copying all characters, add a null terminator (\0) to mark the end of the concatenated string.

    // Add null terminator to the concatenated string
    *dest = '\0';
    

Write a C program to concatenate two strings using pointers

Here's the complete C program to concatenate two strings using pointers:

#include <stdio.h>

void stringConcat(char *dest, const char *src) {
    // Move the pointer to the end of the destination string
    while (*dest != '\0') {
        dest++;
    }

    // Copy characters from src to dest
    while (*src != '\0') {
        *dest = *src;
        src++;
        dest++;
    }

    // Add null terminator to the concatenated string
    *dest = '\0';
}

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

    // Concatenate strings using pointers
    stringConcat(str1, str2);

    // Print the concatenated string
    printf("Concatenated string: %s", str1);

    return 0;
}

Here the original string will be modified and the given string will be merged into the original string. We can also write a program without modifying the original string.

Write a C Program to return a new concatenated string

We can also write this program in such a way that original given string will not be modified and the functions return a new string.

#include <stdio.h>

char* stringConcat(const char* str1, const char* str2) {
    // Calculate the length of the concatenated string
    size_t len1 = strlen(str1);
    size_t len2 = strlen(str2);
    size_t totalLen = len1 + len2;

    // Define a static array to hold the concatenated string
    static char result[100]; // Assuming a maximum length for the concatenated string

    // Copy characters from str1 to the result array
    size_t i, j = 0;
    for (i = 0; i < len1; i++, j++) {
        result[j] = str1[i];
    }

    // Concatenate str2 to the result array
    for (i = 0; i < len2; i++, j++) {
        result[j] = str2[i];
    }

    // Add null terminator at the end
    result[j] = '\0';

    return result;
}

int main() {
    const char* str1 = "Hello, ";
    const char* str2 = "world!";

    // Concatenate strings and store the result in a new string
    char* concatenated = stringConcat(str1, str2);

    // Print the concatenated string
    printf("Concatenated string: %s", concatenated);

    return 0;
}

In this version of the program:

  • We define a static character array result to hold the concatenated string. The size of this array should be large enough to accommodate the maximum possible length of the concatenated string.
  • We loop through each character of str1 and str2 individually and copy them to the result array.
  • After copying all characters, we manually add the null terminator at the end of the concatenated string.
  • Since we are using a static array, the memory is not dynamically allocated, and there is no need to free it explicitly.

However, using a static array has limitations, such as a fixed size and the risk of buffer overflow if the concatenated string exceeds the allocated size. Therefore, it's essential to ensure that the size of the result array is sufficient for the concatenated strings.

Output

Concatenated string: Hello, world!