How to concatenate Strings in C

C-String-Programs

In this article, we are going to learn how to concatenate strings in the C language. We will learn to do this with the help of multiple techniques like:

3 Ways of how to concatenate Strings in C


  • Concatenate Strings in C Using strcat() and strcpy() functions
  • Concatenate Strings in C By Using memccpy() function
  • Concatenate Strings in C By Using Custom Defined Function

1. Concatenate Strings in C Using strcat() and strcpy() functions


In this example, how to concatenate Strings in C by using strcat() and strcpy() functions to concatenate strings. These functions are available in C Standard library and we can use them by just including the header file in our program.

  • strcat() function takes two char* strings and concatenates the second string at the end of the first string,
  • which results in a concatenated string.strcpy() function also takes two char* strings as input and it copies the second string to the first string.
  • let us see in our code example below how we are using these two functions to concatenate strings in C language program
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main() 
{
    const char* str1 = "Welcome to ";
    const char* str2 = "DevEnum Tutorials";

    char Joinedstr[256];
    strcat(strcpy(Joinedstr, str1), str2);

    printf("The Concatination of %s and %s becomes: %s\n", str1,str2,Joinedstr);

    char Joinedstr1[256];
    strcat(strcpy(Joinedstr1, "Welcome to "), "DevEnum Tutorials");
 
    printf("The Concatinated String is : %s\n", Joinedstr1);

    return 0;
}

Output

The Concatination of Welcome to  and DevEnum Tutorials becomes: Welcome to DevEnum Tutorials
The Concatinated String is : Welcome to DevEnum Tutorials

2. Concatenate Strings in C By Using memccpy() function


In this example, we are making use of another C Standard library function memccpy() to concatenate two strings. This function takes a source string, a destination string, and also information about how many bytes we want to copy from source to destination. let us see with the below code example how to use the memccpy() function to concatenate strings in C language.

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

int main() {
    const char* str1 = "Welcome to ";
    const char* str2 = "DevEnum Tutorials";

    char Joinedstr[256];
    memccpy(memccpy(Joinedstr, str1, '
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main() {
    const char* str1 = "Welcome to ";
    const char* str2 = "DevEnum Tutorials";

    char Joinedstr[256];
    memccpy(memccpy(Joinedstr, str1, '\0', 256) - 1, str2, '\0', 256);
    printf("The Concatinated String is : %s\n", Joinedstr);

    return 0;
}

', 256) - 1, str2, '
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main() {
    const char* str1 = "Welcome to ";
    const char* str2 = "DevEnum Tutorials";

    char Joinedstr[256];
    memccpy(memccpy(Joinedstr, str1, '\0', 256) - 1, str2, '\0', 256);
    printf("The Concatinated String is : %s\n", Joinedstr);

    return 0;
}

', 256); printf("The Concatinated String is : %s\n", Joinedstr); return 0; }

Output

The Concatinated String is : Welcome to DevEnum Tutorials

3. Concatenate Strings in C By Using Custom Defined Function


In this example, we are not using any Standard library functions, instead, we are writing our own custom function to concatenate strings. This is exactly the same like we made use of memccpy() function in the above example, here we are writing our own flavor of memccpy(). Let us understand this with the code example below.

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


void *JoinStrings(void* restrict dst, const void* restrict src, int ch, int len)
{
    const char *s = src;
    for (char *ret = dst; len; ++ret, ++s, --len)
    {
        *ret = *s;
        if ((unsigned char)*ret == (unsigned char)ch)
            return ret + 1;
    }
    return 0;
}

int main() 
{
    const char* str1 = "Welcome to ";
    const char* str2 = "DevEnum Tutorials";

    char Joinedstr[256];

    JoinStrings(JoinStrings(Joinedstr, str1, '
include <stdio.h>
#include <string.h>
#include <stdlib.h>


void *JoinStrings(void* restrict dst, const void* restrict src, int ch, int len)
{
    const char *s = src;
    for (char *ret = dst; len; ++ret, ++s, --len)
    {
        *ret = *s;
        if ((unsigned char)*ret == (unsigned char)ch)
            return ret + 1;
    }
    return 0;
}

int main() 
{
    const char* str1 = "Welcome to ";
    const char* str2 = "DevEnum Tutorials";

    char Joinedstr[256];

    JoinStrings(JoinStrings(Joinedstr, str1, '\0', 256) - 1, str2, '\0', 256);
    printf("The Concatinated String is : %s\n", Joinedstr);

    return 0;
}
', 256) - 1, str2, '
include <stdio.h>
#include <string.h>
#include <stdlib.h>


void *JoinStrings(void* restrict dst, const void* restrict src, int ch, int len)
{
    const char *s = src;
    for (char *ret = dst; len; ++ret, ++s, --len)
    {
        *ret = *s;
        if ((unsigned char)*ret == (unsigned char)ch)
            return ret + 1;
    }
    return 0;
}

int main() 
{
    const char* str1 = "Welcome to ";
    const char* str2 = "DevEnum Tutorials";

    char Joinedstr[256];

    JoinStrings(JoinStrings(Joinedstr, str1, '\0', 256) - 1, str2, '\0', 256);
    printf("The Concatinated String is : %s\n", Joinedstr);

    return 0;
}
', 256); printf("The Concatinated String is : %s\n", Joinedstr); return 0; }

Output

The Concatinated String is : Welcome to DevEnum Tutorials