Javatpoint Logo
Javatpoint Logo

How to concatenate two strings in c++?

This section will discuss the concatenation of two or more strings in the C++ programming language. The concatenation of the string means the group of characters that combines two more strings to return a concatenated single string. While concatenating the strings, the second string is added at the end of the first string to make a single string.

For example, we have two strings, 'Java' and 'Tpoint', and we want to concatenate to make a single string as Java + Tpoint = JavaTpoint.

How to concatenate two strings in c++

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

  1. Concatenate two strings using for loop
  2. Concatenate two strings using while loop
  3. Concatenate two strings using the + operator
  4. Concatenate two strings using the strcat() function
  5. Concatenate two strings using the append() function
  6. Concatenate two strings using inheritance
  7. Concatenate two strings using friend function and strcat() function

Program to concatenate two strings using for loop

Let's consider an example to combine two strings using for loop in the C++ programming.

Program.cpp

Output

  Enter the first string: Java
  Enter the second string: Tpoint
 The Concatenation of the string Java and Tpoint is JavaTpoint

Program to concatenate two strings using while loop

Let's consider an example to combine two strings using a while loop in C++ programming.

Program2.cpp

Output

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

Program to concatenate two strings using the + operator in C++

+ Operator: It is an arithmetic '+'operator that simply adds two strings to return a new concatenated string.

Let's consider an example to combine two strings using the + operator in C++ programming.

Program3.cpp

Output

Enter the first string: Java
 Enter the second string: Tpoint
 The concatenated string Java and Tpoint is: JavaTpoint

Program to concatenate two strings using the strcat() method

strcat() function: The strcat is an inbuilt function of the string class, which adds two character strings to return a concatenated string.

Syntax

There are two character arrays in the above syntax, arr1 and arr2, which passed inside the strcat() function to return a concatenated string.

Let's consider an example to combine two strings using strcat() function in the C++ programming.

Program4.cpp

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

append() function: An append() function is a predefined library function used to insert or add a second string at the end of the first string to return a single string.

Syntax

In the above syntax, the str2 is a second string to pass in the append() function that inserts the str2 string at the end of the str1 string.

Let's consider an example to combine two strings using append() function in the C++ programming.

Program5.cpp

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 consider an example to combine two strings using inheritance in the C++ programming.

Program6.cpp

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 consider an example to combine two strings using friend function and strcat() function in the C++ programming.

Program7.cpp

Output

Enter the first string: javatpoint
 Enter the second string: .com
 The concatenated string: javatpoint.com






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA