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 ' CppTutorial ', and we want to concatenate to make a single string as Java + CppTutorial = JavaCppTutorial.
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 consider an example to combine two strings using for loop in the C++ programming.
Program.cpp
#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
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 consider an example to combine two strings using a while loop in C++ programming.
Program2.cpp
#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
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
#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
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
strcat function: The strcat is an inbuilt function of the string class, which adds two character strings to return a concatenated string.
Syntax
strcat ( char *arr1, char *arr2)
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
#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
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
str1.append(str2);
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
#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
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
#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
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
#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
Enter the first string: javacpptutorial
Enter the second string: .com
The concatenated string: cpptutorial.com