”;
String concatenation is the process of adding an element to an existing element. In this context, string concatenation is the method by which two (or more) strings can be added to one another. Hence, the resultant string is the combination of the initial string and the added string.
There are several methods to concatenate strings in C++, some of which are given as follows −
- Using string::append() function
- Using ”+” operator
- Using strcat() function for C-style strings
- Using for loop
- Using while loop
- Using range based loop
- Using inheritance
- Using friend function with OOPS
These methods are explained in detail in the next few articles of this chapter. So, let’s dive into these concepts.
String Concatenation Using string::append() Function
String is a class defined in <string> header file, and the append() function is an inherited method of that class. This method is used to append or add a given string to an initial string.
Syntax
The following syntax is used to concatenate string using the append() method −
initial_string.append(new_string); initial_string.append(“this is new”);
Parameters
The string::append() function takes a string as a parameter. The string can be passed explicitly or as an object.
Example of append() function
The following exemplar code is used to concatenate string using the append() method −
#include <iostream> using namespace std; int main() { string initial_string("I Love TP."); string new_string(" I am new here."); //using append function with object parameter initial_string.append(new_string); //using append function with explicit string initial_string.append(" Could you help me?"); cout << initial_string << endl; return 0; }
Output
I Love TP. I am new here. Could you help me?
String Concatenation Using ”+” Operator
One of the easiest way to add strings is to use the ”+” operator on two or more strings. This can be done in place (i.e. without creating a new string), or in a new string object. This is one of the newer features of C++ programming language.
Syntax
The following syntax is used to concatenate string using the ”+” opertaor −
new_string=initial_string+added_string; initial_string+=added_string
Here, the new string can be added in place or by creating a new string object.
Example
The following exemplar code is used to concatenate string using the ”+” opertaor −
#include <iostream> using namespace std; int main() { string initial_string("I Love TP."); string new_string(" I am new here."); string a="Hey !!! " + initial_string; //using new string object a+=new_string; //inplace addition a+=" Could you help me? "; cout << a << endl; return 0; }
Output
Hey !!! I Love TP. I am new here. Could you help me?
String Concatenation Using for Loop
We can use a simple for loop from the beginning of the new string to the end of the new string, and for each iteration, we can add that character to the initial string. This can be done in place, or by using a new string object. This type of concatenation is also possible in C-style strings, which are character arrays.
Syntax
The following syntax is used to concatenate string using for loop from beginning of the string to the end −
for(int i=0;i<s.length();i++){ initial+=s[i]; }
Example
The following exemplar code is used to concatenate string using for loop from beginning of the string to the end −
#include <iostream> using namespace std; int main(){ string initial_string("I Love TP."); string new_string(" I am new here."); for(int i=0;i<new_string.size();i++){ initial_string+=new_string[i]; } //using for loop to iterate over new_string cout << initial_string << endl; return 0; }
Output
I Love TP. I am new here.
String Length Using while Loop
We can also use a simple while loop. This loop runs till we reach the end of the string, and at each iteration, we can add the corresponding character to the initial string. This can be done in place, or by using a new string object. This type of concatenation is also possible in C-style strings, which are character arrays.
Syntax
The following syntax is used to concatenate string using while loop from beginning of the string to the end −
for(int i=0;i<s.length();i++){ initial+=s[i]; }
Example
The following exemplar code is used to concatenate string using while loop from beginning of the string to the end −
#include <iostream> using namespace std; int main() { string initial_string("I Love TP."); string new_string(" I am new here."); int i=0; while(new_string[i]!='' ''){ initial_string+=new_string[i]; i++; } //using while loop to iterate over new_string cout << initial_string << endl; return 0; }
String Concatenation Using range based loop
We can also use a range based loop, which will automatically iterate over the whole string and we can add each character to the initial string. This can be done in place, or by using a new string object.
Syntax
The following syntax is used to concatenate string using range based loop from beginning of the string to the end −
for(char c: s){ initial+=c; }
Example
The following exemplar code is used to concatenate string using range based loop from beginning of the string to the end −
#include <iostream> using namespace std; int main() { string initial_string("I Love TP."); string new_string(" I am new here."); for(char c: new_string){ initial_string+=c; } //using range based loop for concatentation cout << initial_string << endl; return 0; }
Output
I Love TP. I am new here.
String Concatenation Using strcat() Function
We can use strcat() function to concatenate strings in C++. But, this method does not work for string objects, it only works for C-style strings, i.e. character arrays. This method is defined in the <string.h> header file.
Syntax
The following syntax is used to concatenate string using the strcat() method −
strcat(s1,s2);
Parameters
Here, s1 and s2 are two character arrays (i.e. strings) which are passed as parameters to the strcat() method.
Example
The following exemplar code is used to concatenate string using the strcat() method −
#include <bits/stdc++.h> using namespace std; int main(){ char s1[]="I love "; char s2[]=" TP. Could you help me? "; //using strcat function to concatenate //result stored in s1 strcat(s1,s2); cout << s1 << endl; return 0; }
Output
I love TP. Could you help me?
String Concatenation Using Inheritance
We can use strcat() function to concatenate strings in C++. But, this method does not work for string objects, it only works for C-style strings, i.e. character arrays. This method is defined in the <string.h> header file.
Syntax
The following syntax is used to concatenate string using the inheritance method in OOP concepts −
strcat(s1,s2);
Example
The following exemplar code is used to concatenate string using the inheritance method in OOP concepts −
#include <bits/stdc++.h> using namespace std; //parent class class parent { public: virtual string concatenate(string s1, string s2) = 0; //creating a virtual method to inherit }; //child class class child: parent { public: string concatenate (string s1, string s2){ s1+=s2; //using + operator to add strings return s1; } }; int main() { child ch1; cout << ch1.concatenate ("I love ", "TP !!!"); return 0; }
Output
I love TP !!!
String Concatenation Using Friend Function with OOPS
A friend class can access private and protected members of other classes in which it is declared as a friend. It is sometimes useful to allow a particular class to access private and protected members of other classes.
Hence, we make use of this friend class to declare a helper method, and then use the strcat() function for C-style strings.
Syntax
The following syntax is used to concatenate string using the friend method in C++ −
Class A { string s1=[value]; string s2=[value]; friend void helper(A obj); } helper(A) { strcat(obj.s1, obj.s2); };
Example
The following exemplar code is used to concatenate string using the friend method in C++ −
#include <bits/stdc++.h> using namespace std; // concatenate class class concatenate { public: char s1[20] = "I love TP !!!"; char s2[20] = "Hey... "; friend void helper(concatenate par1); }; void helper (concatenate par1) { // Pass parameter to concatenate strcat (par1.s2, par1.s1); cout << par1.s2; } int main() { // Create object of class concatenate par1; //pass this object to helper function helper(par1); return 0; }
Output
Hey... I love TP !!!
”;