Open In App

C Program to Concatenate Two Strings Without Using strcat

Last Updated : 05 Dec, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

String concatenation is the process of appending one string to the end of another. C language provides strcat() library function to concatenate string but in this article, we will learn how to concatenate two strings without using strcat() in C.

The most straightforward method to concatenate two strings without using strcat() is by using a loop. Let’s take a look at an example:

C
#include <stdio.h>

void concat(char *s1, char *s2) {
    int i = 0;
  
    // Move to the end of str1
    while (s1[i] != '\0')
        i++;

    // Copy characters from str2 to str1
    int j = 0;
    while (s2[j] != '\0') {
        s1[i] = s2[j];  
        i++;
        j++;
    }

    // Null-terminate the concatenated string
    s1[i] = '\0';
}

int main() {
    char s1[50] = "Hello ";
    char s2[] = "Geeks";

    concat(s1, s2);

    printf("%s", s1);
    return 0;
}

Output
Hello Geeks

Explanation: In this method, we iterate through the first string to find its end, then use a loop to append each character of the second string to the end of first string.

There are also a few other methods in C to concatenate two strings without using strcat. Some of them are as follows:

Using Pointers

In this method, we use pointer arithmetic to traverse and concatenate the two strings. First, move the pointer to the end of the first string, then copy each character from second string one by one.

C
#include <stdio.h>

void concat(char *s1, char *s2) {
  
    // Move the pointer to the end of str1
    while (*s1)
        s1++;  

    // Copy characters from s1 to s1 using
  	// pointer arithmetic
    while (*s2) {
        *s1 = *s2;
        s1++;          
        s2++;         
    }

    *s1 = '\0';
}

int main() {
    char s1[50] = "Hello ";
    char s2[] = "Geeks";

  	// Concat string s1 and s2
    concat(s1, s2);

    printf("%s", s1);  
    return 0;
}

Output
Hello Geeks

This method is efficient because it directly works with memory addresses and does not require array indexing, making it a bit more low-level.

Using memcpy()

The memcpy() is a function in C that copies a block of memory from a source location to a destination. It can be used to copy the second-string memory at the end of first string.

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

void concat(char *s1, const char *s2) {

    // Copy s2 to the end of s1 using memcpy()
    memcpy(s1 + strlen(s1), s2, strlen(s2) + 1);
}

int main() {
    char s1[50] = "Hello";
    char s2[] = " Geeks";

    // Concatenate using memcpy()
    concat(s1, s2);

    printf("%s", s1);
    return 0;
}

Output
Hello Geeks

Using sprintf()

The sprintf() function in C can write a formatted string to a string buffer. It can be used to concatenate string by formatting second string at the end of the first string by treating it as a string buffer.

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

int main() {
    char s1[50] = "Hello "; 
    char s2[] = "Geeks";

    // Concatenate s2 to s1 using sprintf
    sprintf(s1 + strlen(s1), "%s", s2);

    printf("%s", s1);
    return 0;
}

Output
Hello Geeks

Next Article

Similar Reads