C++ Program to Concatenate Two Strings

Here you will get C++ program to concatenate two string without using library function.

Below program will read two strings from user and then concatenate them to form third string.

#include<iostream>

using namespace std;

int main()
{
	char str1[30],str2[30],str3[60];
	int i,j;
	cout<<"Enter first string:";
	gets(str1);
	cout<<"\nEnter second string:";
	gets(str2);
	
	for(i=0;str1[i]!='\0';++i)
		str3[i]=str1[i];
		
	for(j=0;str2[j]!='\0';++j)
		str3[i+j]=str2[j];
		
	str3[i+j]='\0';
	
	cout<<"\nThe concatenated string is "<<str3;

	return 0;
}

Output

Enter first string:Hello

Enter second string:World

The concatenated string is HelloWorld

2 thoughts on “C++ Program to Concatenate Two Strings”

Leave a Comment

Your email address will not be published. Required fields are marked *