C++ program to concatenate two string

In this program, you will learn a C++ program to concatenate two strings. In C++ programming string concatenation is the process of joining two or more strings to form a new resultant string. Here we discuss three different approaches to joining strings.

1: Using the “+” operator

2: Using strcat () function

3: Using the append function

Let’s Discuss one by one


1: Using the “+” operator

In c++ this is the easiest way to concatenate two strings. This program takes two strings from the user and concatenates them using the “+” operator and displays the resultant string on the console screen.

#include <iostream>
#include <string>
using namespace std;
 
int main () {
  string string1 = "Hello ";
  string string2 = "World";
  string result = string1 + string2;
  cout << result;
  return 0;
}

Output

Hello World

2: Using strcat () function

The strcat function joins two strings in C++. It is found in the string library. Let’s check the C++ program below.

#include <iostream>
#include <string.h>
using namespace std;
 
int main () {
  char string1[100] = "Hello ";
  char string2[100] = "World";
  strcat (string1,string2);
  cout << string1;
  return 0;
}

Output

Hello World

3: Using the append function

This is the pre-defined library function. It can also be used to join two strings in C++. Below is the C++ program to concatenate two strings using the append function.

#include <iostream>
#include <string>
using namespace std;
 
int main () {
  string string1 = "Hello ";
  string string2 = "World";
  string result = string1.append(string2);
  cout << result;
  return 0;
}

Output

Hello World

Recommended Articles

C++ Program to remove spaces from a string

C++ Strings

Scroll to Top