String Concatenation in C++: A Complete Guide

In this article, we will unveil the various ways of performing string concatenation in the C++ language. This method can be used for various purposes while programming. But in general, the concept is the same as combining two strings from different locations and placing them together.

Techniques of String Concatenation in C++

The following techniques can be taken into consideration while concatenating strings in C++:

  • C++ concatenate (+) operator
  • The strcat() method
  • C++ append() function
  • Using C++ for loop for concatenation

1. C++ ‘+’ operator for String Concatenation

C++ ‘+’ operator can be used to concatenate two strings easily.

The ‘+’ operator adds the two input strings and returns a new string that contains the concatenated string.

Syntax:

Example:

#include <bits/stdc++.h> 
using namespace std; 

int main() 
{   string str1="", str2="";

    cout<<"Enter String 1:\n"; cin>>str1;
    cout<<"Enter String 2:\n"; cin>>str2;
    
    string res = str1 + str2;
    cout<<"Concatenated String:"<<endl;
    cout<<res;
    
	return 0; 
}

Output:

Enter String 1:
Journal
Enter String 2:
Dev
Concatenated String:
JournalDev

2. C++ strcat() method

C++ has a built-in method to concatenate strings. The strcat() method is used to concatenate strings in C++.

Syntax:

strcat(char *array1, char *array2)

Example 1:

#include <bits/stdc++.h> 
using namespace std; 

int main() 
{   
    char str1[100] = "Journal";
    char str2[100]= "Dev";
    
     
    cout<<"Concatenated String:"<<endl;
    
    strcat(str1, str2);
    cout<<str1;
	return 0; 
}

In the above example, we have declared two char arrays mainly str1 and str2 of size 100 characters. Then, we have passed the char array str1 and str2 to the strcat() function to get the concatenated string as a result.

Output:

Concatenated String:
JournalDev

Example 2:

#include <bits/stdc++.h> 
using namespace std; 

int main() 
{   
    char str1[100], str2[100];
    cout << "Enter String 1:\n";
    cin.getline(str1, 100);

    cout << "Enter String 2:\n";
    cin.getline(str2, 100);

     
    cout<<"Concatenated String:"<<endl;
    
    strcat(str1, str2);
    cout<<str1;
	return 0; 
}

In the above example, we accept string input values from the user using the getline() function of C++ which fetches the input from the terminal character by character.

Output:

Enter String 1:
JournalDev-
Enter String 2:
Python
Concatenated String:
JournalDev-Python

3. The append() Method for String Concatenation in C++

C++ has another built-in method: append() to concatenate strings. The append() method can be used to add strings together.

Syntax:

Example:

#include <bits/stdc++.h> 
using namespace std; 

int main() 
{   string str1="", str2="";

    cout<<"Enter String 1:\n"; cin>>str1;
    cout<<"Enter String 2:\n"; cin>>str2;
    
    str1.append(str2);
    cout<<"Concatenated String:"<<endl;
    cout<<str1;
    return 0; 
}

In the above example, we have passed str2 as a parameter to the append() function. Further, the append() functions add the contents of the string object str2 to the end of the contents of string object str1. Thus, serving the purpose of string concatenation.

Output:

Enter String 1:
Journal
Enter String 2:
Dev
Concatenated String:
JournalDev

4. Using C++ for loop

In order to concatenate strings, we can use C++ for loops to serve the purpose without the need of any in-built function.

Example:

#include
#include
using namespace std;

int main()
{

	char x[100]="Journal", y[100]="Dev";
	cout<<"String 1:\n";
    cout<<x<<endl;
    cout<<"String 2:\n";
    cout<<y<<endl;
	int p;
	for(p=0; x[p] != '\0'; p++);//pointing to the index of the last character of x
	
	for(int q=0; y[q] != '\0'; q++,p++)
	{
		x[p]=y[q];
	}
	
	
	x[p]='\0';
    cout<<"Concatenated String:\n";
	cout<<x<<endl;
	
	return 0;
}

In the above snippet of code, we have accepted two char array inputs mainly: x and y, respectively.

Further, we have traversed through the string of x char array till the pointer variable p points to the index of the last character of x.

Then, we traverse through the character input of char array y and concatenate each character of y to x.

In the end, we add a null character (‘\0’) to the end of the char array x which now contains the concatenated string as a result.

Output:

String 1:
Journal
String 2:
Dev
Concatenated String:
JournalDev

Conclusion

Thus, in this article, we have understood the various techniques to concatenate strings in the C++ language. String Concatenation in C++: A Complete Guide

Create a Free Account

Register now and get access to our Cloud Services.

Posts you might be interested in: