Tutorial

String Concatenation in C++: 4 Ways To Concatenate Strings

Published on August 3, 2022
Default avatar

By Safa Mulani

String Concatenation in C++: 4 Ways To Concatenate Strings

While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.

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:

string1 + string2;

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++.

The strcat() function takes char array as input and then concatenates the input values passed to the function.

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. It takes a string as a parameter and adds it to the end of the other string object.

Syntax:

string1.append(string2);

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<iostream>
#include<string.h>
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.


References

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about us


About the authors
Default avatar
Safa Mulani

author

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
JournalDev
DigitalOcean Employee
DigitalOcean Employee badge
January 3, 2022

there is a fifth way char* myFunc(char*pat,char*fil) { char* c = (char*)calloc(len(pat) + len((char*)fil) + 1, 1); memcpy(c, pat, len(pat)); memcpy(c + len(pat), (char*)fil, len((char*)fil)); return c; }

- Ruslan

    Try DigitalOcean for free

    Click below to sign up and get $200 of credit to try our products over 60 days!

    Sign up

    Join the Tech Talk
    Success! Thank you! Please check your email for further details.

    Please complete your information!

    Get our biweekly newsletter

    Sign up for Infrastructure as a Newsletter.

    Hollie's Hub for Good

    Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

    Become a contributor

    Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

    Welcome to the developer cloud

    DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

    Learn more
    DigitalOcean Cloud Control Panel