Write a Program to concatenate strings in C++

Here we will write a simple program to Concatenate Strings in C++ without using any library functions. Let’s follow the simple approach below:

  • Input 2 strings.
  • Go to end of one string and add second after it.
  • put NULL ” at the end of the string.

Let’s write the C++ code for the same.

Checkout the program to Reverse a String in C++

C++ Program to concatenate strings

#include<iostream>
using namespace std;

int main( )
{

    int l;
    char str1[80],str2[80];
    cout<<"Enter first string :";
    gets(str1);
    cout<<"Enter second string :";
    gets(str2);

    for(l=0;str1[l]!='';l++);

    for(int i=0;str2[i]!='';i++)
        str1[l++]=str2[i];

    str1[l]='';

    cout<<"\nThe first string after adding second string content isnn"<<str1;

    return 0;
}

 

OUTPUT:

concatenate strings C++

Leave a Comment