C Program to Concatenate Two Strings

Howdy readers, today you will learn how to concatenate two strings using C Programming language.

C Program to Concatenate Two Strings

A string is a sequence of characters terminated with a null value \0.

This program asks the user to enter two strings then it concatenates two strings. For example: If the user enters two strings ‘hello’ and ‘world’, then after concatenation it becomes helloworld.

We will perform this task using the following methods:

  1. Using Standard Method
  2. Using String Library Function
  3. Using User-defined Function

So, without any delay, let’s begin this tutorial.

C Program to Concatenate Two Strings

C Program

// C Program to Concatenate Two Numbers
#include <stdio.h>
int main(){
    char a[100], b[100];
    int i, j;
    
    // Asking for input
    printf("Enter first string: ");
    scanf("%s", a);
    printf("Enter second string: ");
    scanf("%s", b);
    
    // Concatenating two strings
    for (i = 0; a[i] != '\0'; i++);
    for (j = 0; b[j] != '\0'; j++, i++){
        a[i] = b[j];
    }
    a[i] = '\0';
    
    // Displaying output
    printf("After concatenation: %s", a);
    
    return 0;
}

Output

Enter first string: sloth
Enter second string: coders
After concatenation: slothcoders
C Program to Concatenate Two Strings

Explanation

    char a[100], b[100];
    int i, j;

In this program, we have declared two strings named a and b. And we have also declared two int data type variables named i and j.

    // Asking for input
    printf("Enter first string: ");
    scanf("%s", a);
    printf("Enter second string: ");
    scanf("%s", b);

Then, the user is asked to enter two strings. These strings get stored in the a and b named variables.

    for (i = 0; a[i] != '\0'; i++);
    for (j = 0; b[j] != '\0'; j++, i++){
        a[i] = b[j];
    }
    a[i] = '\0';

This loop iterates with the first letter of the first string, then keeps on iterating until it reaches the end of the second string.

     // Displaying output
    printf("After concatenation: %s", a);

And the string after concatenation is displayed on the screen with the help of printf() function.

C Program to Concatenate Two Strings Using Library Function

// C Program to Concatenate Two String Using Library Function
#include <stdio.h>
#include <string.h>
int main(){
    char str1[100], str2[100];
    
    // Asking for strings
    printf("Enter the first string: ");
    gets(str1);
    printf("Enter the second string: ");
    gets(str2);
    
    strcat(str1, str2);
    
    // Displaying output
    printf("After concatenation: %s", str1);
    
    return 0;
}

Output

Enter the first string: Bat
Enter the second string: Man
After concatenation: BatMan
C Program to Concatenate Two Strings Using Library

Explanation

    strcat(str1, str2);

In C programming language, strcat() function is used to concatenate two strings. It is defined in the string.h header file.

C Program to Concatenate Two String Using User-defined Function

C Program

// C Program to Concatenate Two Strings Using User-defined Function
#include <stdio.h>
void concat(char str1[25], char str2[25]){
    int i, len = 0;
    
    // Length of first string
    for (i = 0; str1[i] != '\0'; i++){
        len++;
    }
    
    // Concatenating second string at the end of first string
    for (i = 0; str2[i] != '\0'; i++){
        str1[len + i] = str2[i];
    }
    str1[len + i] = '\0';
}

int main(){
    char a[25], b[25];
    int i, j;
    
    // Asking for input
    printf("Enter the first string: ");
    scanf("%s", a);
    printf("Enter the second string: ");
    scanf("%s", b);
    
    // Calling out function
    concat(a, b);
    
    // Displaying output
    printf("After Concatenation: %s", a);
    return 0;
}

Output

Enter the first string: Iron
Enter the second string: Man
After Concatenation: IronMan
C Program to Concatenate Two Strings Using Functions

Explanation

void concat(char str1[25], char str2[25]){
    int i, len = 0;
    
    // Length of first string
    for (i = 0; str1[i] != '\0'; i++){
        len++;
    }
    
    // Concatenating second string at the end of first string
    for (i = 0; str2[i] != '\0'; i++){
        str1[len + i] = str2[i];
    }
    str1[len + i] = '\0';
}

In this program, we have defined a custom function named concat which returns the strings after concatenation using for loop.

    // Calling out function
    concat(a, b);

Then, we call this custom function in the main function and print the strings after concatenation using printf() function.

Conclusion

I hope after reading this post, you understand how to write a program to concatenate two strings using C Programming language.

If you face any problem while understanding this program, then let us know in the comment section. We will be glad to solve your query.

Also Read:

Leave a Comment