×

How to concatenate two strings in C++

In the C++ programming language, the concatenation of two or even more strings is covered in this section. The term "string concatenation" refers to a collection of characters that join two or more strings to form a single concatenated string. The second string is attached to the end of the first string to form a single string when concatenating the strings.

What is a String in C++?

In C++, a string is an object that represents a set (or collection) of various characters. Strings in C++ are members of the standard string class (std::string). A string's characters are kept as a collection of bytes in the shared storage regions by the string class. Strings are most commonly applied in the programmes that require text manipulation. In the C++ programming language, we might execute a number of different operations on the strings. For example, we can use reversing, concatenating, or even sending a function as an argument.

Syntax :

The syntax for the creation of a string in C++ is very much sorted and clear. So knowing more about strings, we will now discover various ways to define a string.

  • The best and most easy approach to define a string is to use the string keyword. The string keyword should be used wherever possible because it is simple to write and comprehend. Before the string keyword is used, we should first include the standard string class into our application.
string strng_name = "It’s a C++ string";
  • Then there are the strings in the C style. The following are the several methods for creating C style strings:
char strng_name[8] = {'C', 'o', 'm', 'p', 'u', ‘t', ‘e’, ‘r’, ‘\0'};
//or
char strng_name[8] = “Computer;
  • It is not required to utilise all the space given to a character array (string):
char strng_name[50] = "Computer";

Concatenation of Strings :

Merging two or more strings to generate a resulting string is termed the concatenation of strings. If we desire to concatenate two or more strings, C++ offers us the functionality to achieve the same.

Example :

we have two strings, 'Comp' and 'uter', and we just want to concatenate to form a single string like Comp + uter = Computer.

In C++, there are three major techniques for concatenating the strings. They are :

1.Using strcat() method

To utilise the strcat() method, we have to include the cstring, a header file into our application. The strcat() method uses two character arrays as input. It concatenates the second array to the ending of the first array.

Syntax :

strcat(char_arr1, char_arr2);

It is vital to remember that the strcat() method only takes character arrays as its inputs. We can’t utilise string objects in the method.

Example :

#include <iostream>
#include <cstring>
using namespace std;


int main() {
  char strng1[] = "Strings ";
  char strng2[] = "Concatenated.";


  strcat(strng1, strng2);


  cout << strng1 << endl;


  return 0;
}

Output :

Strings Concatenated.

Explanation :

In the above example, we used the strcat() method to concatenate the 2 given strings strng1 and strng2. Then we printed the strng1 and got the output as concatenated string.

2.Using append() method :

The append() method adds the first string to the second string. The parameters of type string are utilised with this method.

Syntax :

strng1.append(strng2);

Example :

#include <iostream>
using namespace std;


int main() {
  string strng1 = "Strings ";
  string strng2 = "Concatenated.";


  strng1.append(strng2);


  cout << strng1 << endl;


  return 0;
}

Output :

Strings Concatenated.

Explanation :

In the above example, we used the append() method to concatenate the two give strings strng1 and strng2. By using the append() method, we added the strng1 to the strng2 and got the output as a concatenated string.

3.Using '+' operator :

This is the easiest approach to concatenate strings. The + operator combines multiple strings together and then returns the concatenated string.

Syntax :

strng1 + strng2;

Example :

#include <iostream>
using namespace std;


int main() {
  string strng1 = "Strings ";
  string strng2 = "Concatenated.";


  strng1 = strng1 + strng2;


  cout << strng1 << endl;


  return 0;
}

Output :

Strings Concatenated.

Explanation :

In the above example, we used the “+” operator to concatenate the two given strings strng1 and strng2. The added value of the two strings was stored in the string strng1 itself. Finally, the strng1 was printed to get the concatenated string.

Whether to use '+' operator or append() is solely the choice of the coder . The append() method is significantly quicker than + operation. In those applications where the strings are short, even substituting a ‘+’ operator with the append() method has no major impact on the programme’s performance. The strcat() method, in contrast, definitely have some of the potential performance difficulties because of its temporal complexity and the number of times it is called in the application. In order to give a crisp overview, the append() method or the ‘+’ operator are one of the best options anyday.

Conclusion

In C++, we can concatenate strings using one of three methods:

  • the strcat() method
  • the append() method, or
  • the '+' operator.

Related Topics

Single level Inheritance

Inheritance is a fundamental element of C++’s Object-Oriented Programming (OOP). It allows a class (called the derived class) to inherit characteristics and attributes from another class (called the base class)....

5 minutes read.

Inheritance in C++ vs Java

Just like we inherit traits from our parents, object-oriented programming has a concept called inheritance. In terms of object-oriented programming, a class's traits and behaviours, or its data and methods,...

4 minutes read.

Pthreads or POSIX Threads in C++

The thread API for C/C++ is implemented by pthreads or POSIX threads. It enables the multithreading system, which enables parallel and distributed processing, and the creation of new concurrent process...

3 minutes read.

Auto keyword in C++

The primary function of auto keyword is to assign and detect the value of the data type automatically in C++. The compiler knows the data type of the variable by...

2 minutes read.

C++ Infinite loop

The term "infinite loop" refers to a loop that does not terminate the loop according to the condition. In some cases, an infinite loop may be required in programming, or...

4 minutes read.

Singleton Design Pattern in C++

Singleton is similar to the global variable. Singleton helps to have only one object of its kind and provides only single access. One of the key features of the singleton...

2 minutes read.

Print Table Using Do while Loop in C++

Multiplication Table In mathematics, a table is created by multiplying a certain number by all of the counting numbers, i.e., 1, 2, 3, 4, 5, 6, and so on. It is...

4 minutes read.

rand() and srand() in C / C++

In this tutorial, we'll explore the syntax, usage, and examples of the C++ STL functions rand() and srand(). What exactly is rand()? The C++ STL's built-in rand() function is defined in the...

3 minutes read.

Substring in C++

A substring is a function in c++ that is imported from the string library. An element of a string is called a substring. A substring contains two parameters length and...

2 minutes read.

Private Inheritance in C++

Private inheritance is an inheritance in object-oriented programming (OOP) languages where a subclass derives from a superclass. Still, the derived class does not inherit the public and protected members of...

7 minutes read.

Dynamic Constructor in C++

Dynamic constructors create dynamic memory by using a dynamic memory allocator new within the constructor. This allows us to initialize objects dynamically. The term dynamic constructor is used when memory...

2 minutes read.

C++ Call by Reference

Call by Reference is a C++ method for passing arguments to a function that enables us to pass the actual memory address of the parameter rather than a copy of...

4 minutes read.

Constexpr in C++

Constexpr is a keyword in C++ that is used to declare variables or functions as compile-time constants. This means that the value of a constexpr variable or the return value...

3 minutes read.

Method overriding in C++

What is method overriding? Using the same function in derived class as their base class is referred to as function/method overriding in c++. Method overriding is an example of polymorphism. With...

2 minutes read.

Use of Inheritance in C++

What is inheritance in c++? Inheritance is fundamental in object-oriented programming (OOP) languages like C++. It allows a programmer to create a new class (called a derived class) that inherits the...

4 minutes read.

C++ Queue

C++ queue: Queue in C++ is also a container adapter with the functionality of a queue. Queue is just the opposite of the stack in C++ because stack works on...

4 minutes read.

How to enter a name in C++

A name is a string or array of characters or letters. The string is one of the most helpful data types offered by the C++ library. A string helps the...

4 minutes read.

Ways to Copy a Vector in C++

Vectors in C++ are the same as arrays, along with additional outstanding features than them, like array lists in Java programming language. In Vectors, the size constraint is eliminated, which...

5 minutes read.

Type difference of Character literals in C VS C++

Character literals in C: In C, a character literal is represented by a single character enclosed in single quotes, such as 'a' or 'b'. It is of type int. This means...

5 minutes read.

Accumulate() and partial_sum() in C++ STL Numeric header

The C++ STL's numeric library includes the numeric header. This library provides efficient numeric arrays, support for random number generation, and fundamental mathematical operations and types. Several of the numeric...

3 minutes read.