C program to concatenate two strings

Write a C program to concatenate two strings in single string. How to concatenate two strings to one without using strcat() library function. Adding two strings into one without using inbuilt library function. Logic to concatenate two strings in C programming. C program to concatenate two strings using strcat() library function.

Example

Input

Input string1: I love
Input string2: Codeforwin

Output

Concatenated string: I love Codeforwin

Required knowledge

Basic C programming, Loop, String

Must know -

Concatenation of strings

Concatenation of two strings is the process of joining them together to form a new string. Concatenation appends the second string after first string. Concatenation is sometimes also referred as binary addition of strings i.e. + operation.
For example: I love + Codeforwin = I love Codeforwin

Logic to concatenate two strings

Concatenation of two strings is simple copying a string to another. To concatenate two strings str1 and str2, you will copy all characters of str2 at the end of str1.
Below is the step by step descriptive logic to concatenate two string.

  1. Input two string from user. Store it in some variable say str1 and str2. Here we need to concatenate str2 to str1
  2. Find length of str1 and store in some variable say i = length_of_str;.
  3. Run a loop from 0 till end of str2 and copy each character to str1 from the ith index.

Program to concatenate two strings using while loop

/**
 * C program to concatenate two strings 
 */

#include <stdio.h>
#define MAX_SIZE 100 // Maximum string size

int main()
{
    char str1[MAX_SIZE], str2[MAX_SIZE];
    int i, j;


    /* Input two strings from user */
    printf("Enter first string: ");
    gets(str1);
    printf("Enter second string: ");
    gets(str2);


    /* Move till the end of str1 */
    i=0;
    while(str1[i] != '\0')
    {
        i++;
    }

    /* Copy str2 to str1 */
    j = 0;
    while(str2[j] != '\0')
    {
        str1[i] = str2[j];
        i++;
        j++;
    }

    // Make sure that str1 is NULL terminated
    str1[i] = '\0';

    printf("Concatenated string = %s", str1);

    return 0;
}

As I already talked many times in my previous string posts. In C programming NULL is represented using 0. Hence, the above two NULL checking conditions in while loop is unnecessary and can be stripped.

Program to concatenate two strings

/**
 * C program to concatenate two strings 
 */

#include <stdio.h>
#define MAX_SIZE 100 // Maximum string size

int main()
{
    char str1[MAX_SIZE], str2[MAX_SIZE];
    int i, j;

    /* Input two strings from user */
    printf("Enter first string: ");
    gets(str1);
    printf("Enter second string: ");
    gets(str2);

    /* Move till the end of str1 */
    i=-1;
    while(str1[++i]);

    /* Copy str2 to str1 */
    j = 0;
    while(str1[i++] = str2[j++]);

    printf("Concatenated string = %s", str1);

    return 0;
}

Finally, let us get geeky and apply pointer arithmetic on above approach.

Program to concatenate two strings using pointer

/**
 * C program to concatenate two strings using pointer
 */

#include <stdio.h>
#define MAX_SIZE 100 // Maximum string size

int main()
{
    char str1[MAX_SIZE], str2[MAX_SIZE];
    char * s1 = str1;
    char * s2 = str2;

    /* Input two strings from user */
    printf("Enter first string: ");
    gets(str1);
    printf("Enter second string: ");
    gets(str2);

    /* Move till the end of str1 */
    while(*(++s1));

    /* Copy str2 to str1 */
    while(*(s1++) = *(s2++));

    printf("Concatenated string = %s", str1);

    return 0;
}

Using above methods we concatenated two strings manually. However, in real life you should use inbuilt string library function strcat(str1, str2) to concatenate strings. Where str2 is concatenated to str1. The function is present in string.h header file.

Program to concatenate two strings using strcat() function

/**
 * C program to concatenate two strings using strcat()
 */

#include <stdio.h>
#include <string.h>
#define MAX_SIZE 100 // Maximum size of the string

int main()
{
    char str1[MAX_SIZE], str2[MAX_SIZE];

    /* Input two strings from user */
    printf("Enter first string: ");
    gets(str1);
    printf("Enter second string: ");
    gets(str2);

    /* Concatenate str1 with str2 */
    strcat(str1, str2);

    printf("Concatenated string = %s", str1);

    return 0;
}

Output

Enter first string: I love
Enter second string: Codeforwin
Concatenated string = I love Codeforwin

Happy coding 😉

Write your doubts or suggestion. I will try my best to help. You must escape source code before commenting. To format your source code paste your source code inside
<pre><code> ----Your Source Code---- </code></pre>