Skip to content
Related Articles
Get the best out of our app
GeeksforGeeks App
Open App
geeksforgeeks
Browser
Continue

Related Articles

String Concatenation in C++

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

Prerequisite: String in C++

The string is a type of data structure used for storing characters. Concatenating strings in C++ is one of the most discussed topics related to strings. There are multiple methods to concat strings using user-defined methods, and a couple of methods for the concatenation of strings using pre-defined methods. Let’s check on all of these methods.

String Concatenation in C++

 

There are 4 methods to Concatenate String which are as follows:

  1.  Using append( ) function
  2.  Using ‘+’ Operator
  3. Using strcat( ) function
  4.  Using C++ for loop

1. String Concatenation using append() Function

The append() function is a member function of std::string class. Using this function, we can concatenate two std::string objects (C++ style strings) as shown in the below example.

Syntax:

string& string::append (const string& str);
  • str: String to be appended.

Example: 

C++




// C++ Program for string
// concatenation using append
#include <iostream>
  
using namespace std;
  
int main()
{
    string init("this is init");
    string add(" added now");
  
    // Appending the string.
    init.append(add);
  
    cout << init << endl;
    return 0;
}

Output

this is init added now

2. String Concatenation Using ‘+’ Operator

This is the easiest method for the concatenation of two strings. The + operator adds strings and returns a concatenated string. This method only works for C++ style strings (std::string objects) and doesn’t work on C style strings (character array).

Syntax:

string new_string = init + add;

 Example: 

C++




// C++ Program for string
// concatenation using '+' operator
#include <iostream>
  
using namespace std;
  
int main()
{
    string init("this is init");
    string add(" added now");
  
    // Appending the string.
    init = init + add;
  
    cout << init << endl;
    return 0;
}

Output

this is init added now

3. String Concatenation using strcat( ) Function

The C++ strcat( ) function is a built-in function defined in <string.h> header file. This function concatenates the two strings init and add and the result is stored in the init string. This function only works for C style strings (character arrays) and doesn’t work for C++ style strings (std::string objects).

Syntax:

char * strcat(char * init, const char * add);

Example: 

C++




// C++ Program for string
// concatenation using strcat
#include <iostream>
#include <string.h>
  
using namespace std;
  
int main()
{
    char init[] = "this is init";
    char add[] = " added now";
  
    // concatenating the string.
    strcat(init, add);
  
    cout << init << endl;
  
    return 0;
}

Output

this is init added now

4. String Concatenation using Loops

Using a loop is one of the most basic methods of string concatenation. Here, we are adding elements one by one while traversing the whole string and then another string.  The final result will be the concatenated string formed from both strings.

Example: The below examples shows the C++ program to concatenate two strings.

C++




// C++ Program for string
// concatenation using for loop
#include <iostream>
  
using namespace std;
  
int main()
{
    string init("this is init");
    string add(" added now");
  
    string output;
  
    // Adding element inside output
    // from  init
    for (int i = 0; init[i] != '\0'; i++) {
        output += init[i];
    }
  
    // Adding element inside output
    // fromt add
    for (int i = 0; add[i] != '\0'; i++) {
        output += add[i];
    }
  
    cout << output << endl;
    return 0;
}

Output

this is init added now

My Personal Notes arrow_drop_up
Last Updated : 11 Mar, 2023
Like Article
Save Article
Similar Reads