C Program to Concatenate Two Strings


Posted On: Dec 15, 2020

C Program to Concatenate Two Strings



As we know that time is the most important concern in our life. To do any task easily, We have to use software to perform it in a simpler manner. Software is a set of programs with related documents. The Software Program or Program is a set of instructions to do a specific task. Here, our specific task is to Concatenate Two Strings. First, I want to explain String and Concatenation of Strings. Furthermore, I would like to demonstrate how to concatenate two strings. After all, I will write the logic of a C program to reverse a string and explain it with output.

Table of contents:

  1. What is a String?
  2. What is Concatenation of String?
  3. Demonstration to Concatenate Two Strings
  4. Logic to Concatenate Two Strings
  5. C program to reverse a string
  6. Conclusion

What is a String?

String is a combination of characters that may be any digit from 0 to 9 and Alphabets from A to Z. The repetition of characters does not matter. For instance, a String Conax uses 5 characters i.e. C,o,n,e,and x. Strings are actually one-dimensional arrays of characters terminated by a < strong > null character '\0'. Thus a null-terminated string contains the characters that comprise the string followed by a null.

What is Concatenation of String?

Concatenation is the process of appending one string to the end of another string. You can concatenate strings by using the + operator to add them together to make a new string. For instance, If First String is Welcome and second is User then concatenation of these two strings is Welcome User. Concatenation < strong > appends the second string after the first string.

Demonstration to Concatenate Two Strings

Concatenate Strings

Logic to Concatenate Two Strings

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 strings:

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

C program to reverse a string

 * 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: ");
    printf("Enter second string: ");

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

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

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

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

    return 0;

Save this program with the filename and extension .c like filename.c. Then compile it on turbo C or another compiler.

The output of the Program:

Enter first string: Welcome

Enter second string: User

Concatenated string = WelcomeUser

Concatenate Strings Output


The internal working of this program depends upon the length of the strings.

After the successful compilation of the program, a message is displayed on the screen as Enter first string:

User Enter Welcome

Then, again a message displayed Enter second string:

If second time user entered User

Then, the final output is Concatenated string is WelcomeUser.

    Related Questions

    Please Login or Register to leave a response.

    Related Articles

    C Program Examples

    C Program to convert Decimal to Binary Number

    A Decimal Number is constructed with any digit from 0 to 9. For instance, 24 is a Decimal Number constructed from the digits 2 and 4. A Binary Number is constructed with digits 0 and 1. For instance, ..

    C Program Examples

    C program to reverse a string

    This program reversed the entered string that means opposite of the previous string sequence...

    C Program Examples

    C program to reverse an Integer number

    Are you want a demonstration of The C Program that reverses an Integer number? This Program reversing the sequence of digits in an Integer. After the successful compilation of the program, a message..