How To Concatenate Two Strings In C++ - C++ Programming Tutorial
C++ Course / Strings / How To Concatenate Two Strings In C++

How To Concatenate Two Strings In C++

BLUF: Mastering How To Concatenate Two Strings In C++ is a critical step in becoming a proficient C++ developer. This lesson provides a deep dive into the syntax, performance considerations, and real-world applications of this concept.
Key Performance Insight: How To Concatenate Two Strings In C++

C++ is renowned for its efficiency. Learn how How To Concatenate Two Strings In C++ enables low-level control and high-performance computing in the tutorial below.

This part will cover the merging of multiple strings in the C++ coding language. String concatenation involves merging two or more strings to produce a unified string. During the concatenation process, the second string is appended to the end of the first one to form a cohesive string.

For instance, consider two strings, ' Python ' and ' Programming ', that we aim to merge to form a combined string: Python + Programming = PythonProgramming.

Let's discuss the different ways to concatenate the given string in the C++ programming language.

  • Concatenate two strings using for loop
  • Concatenate two strings using while loop
  • Concatenate two strings using the + operator
  • Concatenate two strings using the strcat function
  • Concatenate two strings using the append function
  • Concatenate two strings using inheritance
  • Concatenate two strings using friend function and strcat function
  • Program to concatenate two strings using for loop

Let's explore an illustration of merging two strings using a for loop in C++ programming.

Program.cpp

Example

#include <iostream>
using namespace std;
int main ()
{
string str1, str2, result; // declare string variables
int i;
cout <<"  Enter the first string: ";
cin >> str1; // take string
cout << "  Enter the second string: ";
cin >> str2; // take second string
// use for loop to enter the characters of the str1 into result string 
for ( i = 0; i < str1.size(); i++)
{
result = result + str1[i]; // add character of the str1 into result 
} 

// use for loop to enter the characters of the str2 into result string 
for ( i = 0; i < str2.size(); i++)
{
result = result + str2[i]; // add character of the str2 into result 
}
cout << " The Concatenation of the string " << str1 << " and " << str2 << " is " <<result;
return 0;
}

Output

Output

Enter the first string: Java
  Enter the second string: CppTutorial
 The Concatenation of the string Java and CppTutorial is JavaCppTutorial

Program to concatenate two strings using while loop

Let's explore a demonstration of merging two strings by utilizing a while loop in C++ programming.

Program2.cpp

Example

#include <iostream>
using namespace std;
int main ()
{
	// declare and initialize the string 
	char str1[100] = " We Love";
	char str2[100] = " C++ Programming Language";
	int i, j; // declare variable
	
	cout << " The first string is: " << str1 << endl;
	cout << " The second string is: "<< str2 <<endl;
	for (i = 0; str1[i] != '\0'; i++);
	j = 0; // initialize j with 0;
	
	// use while loop that insert the str2 characters in str1
	while (str2[j] != '\0') // check str2 is not equal to null
	{
		str1[i] = str2[j]; // assign the character of str2 to str1
		i++;
		j++;	
	}
	str1[i] = '\0'; 
	cout << " The concatenated string is: " << str1;
	return 0;
}

Output

Output

The first string is:  We Love
 The second string is:  C++ Programming Language
 The concatenated string is:  We Love C++ Programming Language

The ### Program to concatenate two strings using the + operator in C++ operator performs string concatenation by adding two strings together to create a new concatenated string.

Let's explore an illustration where we merge two strings by utilizing the + operator in C++ programming.

Program3.cpp

Example

#include <iostream>
using namespace std;
int main ()
{
	string str1, str2; // declare string variables
	
	cout << " Enter the first string: ";
	cin >> str1;
	
	cout << "\n Enter the second string: ";
	cin >> str2;
	
	// use '+' operator to concatenate the str1 and str2
	string result = str1 + str2;
	cout << " The concatenated string " << str1 << " and " << str2 <<" is: " << result;
	return 0;
}

Output

Output

Enter the first string: Java
 Enter the second string: CppTutorial
 The concatenated string Java and CppTutorial is: JavaCppTutorial

Program to concatenate two strings using the strcat method

The strcat function is a built-in feature of the string class that combines two character strings to produce a single concatenated string.

Syntax

Example

strcat ( char *arr1, char *arr2)

There are two arrays of characters, namely arr1 and arr2, which are provided as arguments to the strcat function in order to produce a combined string.

Let's explore a scenario where we merge two strings by employing the strcat function in C++ programming.

Program4.cpp

Example

#include <bits/stdc++.h>
#include <string>
using namespace std;
int main()
{
	// declare and initialize the string
	char str1[] = " We love";
	char str2[] = " C++ Programming";

	cout << " String 1: " <<str1 <<endl;
	cout << " String 2: " <<str2 <<endl;	
	// use the strcat() function to concatenate the string
	strcat(str1, str2);

	cout << " The concatenated string is: " <<str1;
	return 0;
}

Output

Output

String 1:  We love
String 2: C++ Programming
 The concatenated string is:  We love C++ Programming

Program to concatenate two strings using the append function

The append method is a built-in function that is employed to concatenate a second string at the conclusion of the first string in order to produce a unified string.

Syntax

Example

str1.append(str2);

In the provided syntax, the variable str2 represents a secondary string parameter passed into the append method, which effectively appends the content of str2 to the end of the str1 string.

Let's explore an illustration of merging two strings by employing the append method in C++ programming.

Program5.cpp

Example

#include <iostream>
using namespace std;
int main ()
{
string str1, str2, result; // declare string variables

cout <<"  Enter the first string: ";
cin >> str1; // take string
cout << "  Enter the second string: ";
cin >> str2; // take second string

// use append() function to insert element at the end of the str1
str1.append(str2);
cout << " \n The concatenation of the string is: "<< str1;
return 0;
}

Output

Output

Enter the first string: Hello
  Enter the second string: Friends!

 The concatenation of the string is: HelloFriends!

Program to concatenate two string using the inheritance of the class

Let's explore a demonstration of merging two strings by employing inheritance in C++ programming.

Program6.cpp

Example

#include <iostream>
#include <string>
using namespace std;

// create base class
class base
{
	protected:
		virtual string concatenate(string &str1, string &str2) = 0;
};	

// create derive class to acquire features of base class
class derive: protected base {
	public:
		string concatenate (string &str1, string &str2) {
			string temp;
			temp = str1 + str2;
			return temp;
		}
};

int main()
{
	// declare variable
	string str1, str2;
	
	cout <<" Enter first string: ";
	cin >>str1;
	cout <<" \n Enter second string: ";
	cin >>str2;
	
	// create string object
	derive obj;
	
	// print string
	cout <<" \n The concatenated string is: " << obj.concatenate (str1, str2);
	
	return 0;
}

Output

Output

Enter first string: C++

 Enter second string: Programming

 The concatenated string is: C++Programming

Program to concatenate two strings using the friend function and strcat function

Let's explore an illustration demonstrating the merging of two strings by employing a friend function alongside the strcat function within the realm of C++ programming.

Program7.cpp

Example

#include <iostream>
#include <cstring>
using namespace std;

class Base {
	private:
		char st[100], st2[100];
	
	public:
	
		void inp()
		{
			cout <<" Enter the first string: ";
			cin.getline (st, 100); // take a line of string with limit 100
		
			cout <<" Enter the second string: ";
			cin.getline (st2, 100); // take a line of string with limit 100
		
			
			
		friend void myfun(Base b);		
};

void myfun (Base b)
{
	strcat (b.st, b.st2); // pass parameter to concatenate
	
	cout <<" The concatenated string: " <<b.st;
}

int main()
{
	
	Base b; // create b as an object
	
	b.inp(); // b object call inp() function
	myfun(b); // pass b object to myfun() to print the concatenated string
	
	return 0;
}

Output

Output

Enter the first string: javacpptutorial
 Enter the second string: .com
 The concatenated string: cpptutorial.com

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience