Skip to content
Related Articles

Related Articles

Concatenating Two Strings in C

View Discussion
Improve Article
Save Article
  • Difficulty Level : Easy
  • Last Updated : 14 Sep, 2022
View Discussion
Improve Article
Save Article

Given two strings str1 and str2, our task is to concatenate these two strings. There are multiple ways to concatenate two strings in C language:

  • Without Using strcat() function
    • Using standard method
    • Using function
    • Using recursion
  • Using strcat() function

1. Concatenating Two strings without using the strcat() function

A. Using Standard Method

Input: str1 = "hello", str2 = "world"
Output: helloworld

Input: str1 = "Geeks", str2 = "World"
Output: GeeksWorld

Approach:

  • Get the two Strings to be concatenated
  • Declare new Strings to store the concatenated String
  • Insert the first string into the new string
  • Insert the second string in the new string
  • Print the concatenated string

Below is the implementation of the above approach: 

C




// C Program to concatenate two 
// strings without using strcat
#include <stdio.h>
    
int main()
{
    
    // Get the two Strings to be concatenated
    char str1[100] = "Geeks", str2[100] = "World";
    
    // Declare a new Strings
    // to store the concatenated String
    char str3[100];
    
    int i = 0, j = 0;
    
    printf("\nFirst string: %s", str1);
    printf("\nSecond string: %s", str2);
    
    // Insert the first string 
    // in the new string
    while (str1[i] != '\0') {
        str3[j] = str1[i];
        i++;
        j++;
    }
    
    // Insert the second string 
    // in the new string
    i = 0;
    while (str2[i] != '\0') {
        str3[j] = str2[i];
        i++;
        j++;
    }
    str3[j] = '\0';
    
    // Print the concatenated string
    printf("\nConcatenated string: %s", str3);
    
    return 0;
}

Output:

First string: Geeks
Second string: World
Concatenated string: GeeksWorld

B. Using Function

Approach:

  • main function will call concatenate_string() function to concatenate two strings. 
  • The function will get the length of string s with the help of strlen. 
  • Now we will append the character of string s1 at s[i+j]. This step will be repeated till no character is available in s1. We are appending characters of string s1 to s from the end of s.
  • After for loop, we will be concatenating the string s.
  • At last main function will print the string which is concatenated.

C




// C program to concatenating two 
// strings using function
#include <stdio.h>
#include <string.h>
  
void concatenate_string(char* s, char* s1)
{
    int i;
  
    int j = strlen(s1);
  
    for (i = 0; s1[i] != '\0'; i++) {
        s[i + j] = s1[i];
    }
  
    s[i + j] = '\0';
  
    return;
}
  
int main()
{
  
    char s[5000], s1[5000];
  
    printf("Enter the first  string: ");
    gets(s);
  
    printf("Enter the second string: ");
    gets(s1);
  
    // function concatenate_string 
    // called and s and s1 are
    // passed
    concatenate_string(s, s1);
  
    printf("Concatenated String is: '%s'\n", s);
  
    return 0;
}

Output:
Enter the first string: Geeks
Enter the second string: forGeeks
Concatenated String is: GeekforGeeks

C. Using Recursion

Approach:

  • The function concatenate_string() will get the strings s and s1.
  • if no elements are present in s1 then assign s1 with a null (\0) character.
  • else if elements are present then we will add the element of string s1 at the end of the string s and will increase the value of i by 1.
  • The function concatenate_string will call itself by passing the modified strings s, s1 as arguments. This function will call itself recursively until no elements are available in s1.

C




// C program to concatenate two 
// strings with the help of 
// recursion
#include <stdio.h>
#include <string.h>
  
void concatenate_string(char* s, char* s1)
{
  
    static int i = 0;
    static int j = strlen(s);
  
    if (!s1[i]) {
        s1[i] = '\0';
    }
    else {
        s[i + j] = s1[i];
        i++;
        concatenate_string(s, s1);
    }
}
int main()
{
  
    char s[5000], s1[5000];
  
    printf("Enter the first  string: ");
    gets(s);
  
    printf("\nEnter the second string: ");
    gets(s1);
  
    // function concatenate_string 
    // called and s1 and s2 are
    // passed
    concatenate_string(s, s1);
  
    printf("\nConcatenated String is: '%s'\n", s);
  
    return 0;
}

Output:
Enter the first string: Geeks
Enter the second string: forGeeks
Concatenated String is: GeeksforGeeks

2. Using strcat() function

strcat() function in C appends the copy of the source string to the destination with a Null character at the end of the string. It comes under string.h header file in C.

C




// C program to concatenate two 
// strings using strcat function
#include <stdio.h>
#include <string.h>
  
int main()
{
  
    char s[] = "Geeks";
    char s1[] = "forGeeks";
  
    // concatenating the string
    strcat(s, s1);
    printf("Final string is: %s ", s);
    return 0;
}

Output

Final string is: GeeksforGeeks 

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!