How to concatenate two strings in C

In this blog post, you will learn to concatenate two strings in C with and without the C library functions.

C string is a sequence of characters terminated with a null character ‘\0’. Concatenating two strings means joining one string to another string. For example,

Input: 
string1 ="Aticle", string2="World"


Output:
AticleWorld

 

The primary prerequisite to understanding the below example code:

 

Now for a better understanding let’s see a problem statement. It also gives you the idea that how people ask questions in the interview related to string concatenation.

 

Problem Statment: Given two C strings concatenate one string to another and print the output string.

 

Now let’s see the few ways to solve this problem but it is my recommendation that first try it yourself before jumping directly on the solution.

 

Solution 1: Using the strcat()

You can use the strcat function to concatenate two strings in C. It appends a copy of the string to the end of the destination string (including the terminating null character). The initial character of the source string overwrites the null character at the end of the destination string.

#include <stdio.h>
#include <string.h> /* declares the strcat() function */

#define SIZE 20

int main()
{
    char dest[SIZE]="Aticle";

    char src[SIZE]="World";

    strcat(dest,src);

    printf("Concatenated String: %s\n", dest);

    return 0;
}

Output:

Concatenated String: AticleWorld

 

Note: The size of the destination array must be large enough to store the resultant string otherwise strncat shows undefined behavior.

 

Solution 2: Using the strncat()

If you don’t want to append the complete string and only want to append n characters, the strncat function will help you.

It appends “n” characters from the source string to the destination string. The initial character of the source string overwrites the null character at the end of the destination string.

#include <stdio.h>
#include <string.h> /* declares the strncat() function */

#define SIZE 20

int main()
{
    char dest[SIZE]="Aticle";

    char src[SIZE]="World";

    strncat(dest,src,2);

    printf("Concatenated String: %s\n", dest);

    return 0;
}

Output:

Concatenated String: AticleWo

 

Solution 3: Without library function

You can simply append one string to another using the C loop. You just need to remember that the destination buffer must have enough space to append the second string.

Here we follow a simple approach to append the string.

Step-1: Reach the end of the first string.

Step-2: Start traversing the second string and keep appending the elements to the first string.

Step-3: Stop the traversing and appending character when the number of appended characters is equal to the given n (number of characters which you want to append) or the end of the second string comes.

 

Note: Below C program is not validating the source and destination buffer and assuming that the destination buffer has enough space to hold the appended string.

#include <stdio.h>

#define SIZE 20



char *appendString(char *dest, const char *src, unsigned int n)
{
    //Pointer should not null pointer
    if((dest == NULL) && (src == NULL))
        return NULL;

    //Find the end of the destination string
    while(*dest != '\0')
    {
        dest++;
    }

    //Now append the source string characters
    //until not get null character of src or n != 0
    while (n--)
    {
        //when null char comes it returnappendString
        if (!(*dest++ = *src++))
        {
            return dest;
        }
    }

    //Append null character in the last
    // when n become 0 before ending the src
    *dest = '\0';
    return dest;
}

int main()
{
    char dest[SIZE]="Aticle";
    char src[SIZE]="World";

    appendString(dest,src,3);

    printf("Concatenated String: %s\n", dest);

    return 0;
}

Output:

Concatenated String: AticleWor

 

Solution 4: Multiple String Concatenation

You can append multiple strings by calling the string appended function multiple times. For example,

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

#define SIZE 100



int main()
{
    //Make sure it have enough space
    char outBuf[SIZE];

    char s1[] = "I "; //string1
    char s2[] = "Love "; //string2
    char s3[] = "To "; //string3
    char s4[] = "Read "; //string4
    char s5[] = "Aticleworld"; //string5

    strcpy(outBuf,s1);
    strcat(outBuf, s2);
    strcat(outBuf, s3);
    strcat(outBuf, s4);
    strcat(outBuf, s5);

    printf("Concatenated String: %s\n", outBuf);

    return 0;
}

Output: Concatenated String: I Love To Read Aticleworld

 

But the above-mentioned code is not much efficient because in every calling strcat has to walk every character in both the source and destination buffer.

So now question is that if the above code is not an efficient way to append the multiple strings then what is the better way?

Don’t worry I have the answer for you. You can use the snprintf(). If you are sure about the destination buffer, you can use snprintf to append the multiple strings.

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

#define SIZE 100



int main()
{
    //Make sure it have enough space
    char outBuf[SIZE];

    char s1[] = "I "; //string1
    char s2[] = "Love "; //string2
    char s3[] = "To "; //string3
    char s4[] = "Read "; //string4
    char s5[] = "Aticleworld"; //string5

    snprintf(outBuf, sizeof(outBuf), "%s%s%s%s%s",s1,s2,s3,s4,s5);

    printf("Concatenated String: %s\n", outBuf);

    return 0;
}

Output: Concatenated String: I Love To Read Aticleworld

 

Recommended Post:

Leave a Reply

Your email address will not be published. Required fields are marked *