C Program to concatenate two strings without using strcat

Given two strings str1 and str2, the task is to write a C Program to concatenate these two strings without using the strcat() function

Examples:

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

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

Approach:



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

Below is the implementation of the above approach:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Output:

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


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.