×

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

C++ Switch

In C++, an expression or variable can be tested against a range of constant values using a switch statement, which is a control flow statement. It offers a productive method...

4 minutes read.

C++ Comments

Comment/Remark A Comment or a Remark is text that is ignored by the compiler yet is beneficial to programmers. Code is usually annotated with comments for future reference. They are treated...

3 minutes read.

Passing by Reference Vs. Passing by the pointer in C++

 Passing by Reference Vs. Passing by the pointer in C++ Throughout C++, it can transfer parameter values except by pointers or through referring to a function. For both cases, we have...

3 minutes read.

Depth First Search Program to Traverse a Graph in C++

Depth First Search (DFS) is a technique that is used for transversing the graph. The process of Depth First Search (DFS) starts from the root node and then next comes...

6 minutes read.

C++ Constructor

Constructor is a specific method in C ++ that is automatically called when an object is created. Typically, it is used to set the data members of a new object....

4 minutes read.

Diamond Pattern using While-loop in C++

What is while Loop? A while loop or while statement repeats all code of its body as long as a specific condition is satisfied. The loop ends if or when the...

5 minutes read.

Bits stdc++.h in C++

<bits/stdc++.h> in C++ In essence, it is a header file that contains all the standard libraries. It makes sense to use this file in programming competitions to speed up work, especially...

2 minutes read.

Difference between Exit and Return

Define Exit() At the point when a client needs to leave a program from this capability is utilized. A void return type capability calls all capabilities enrolled at the exit and ends...

3 minutes read.

C++ Program For FCFS (First Come First Serve)

The most basic scheduling technique is FCFS, often known as "FIFO (First In, First Out)". In this procedure, the first one is utilized and executed first, while the second one...

4 minutes read.

C++ Scope of Variables

In this tutorial, we will explore about the scope of variables in c++ programming language. And also, how it works in a program. What is Scope? The range of applications for something...

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.

Desired Capabilities in Selenium Web Driver in C++

A class called Desired Capabilities is used to specify a set of fundamental requirements, such as browser, operating system, and version combinations.to carry out automated testing of a web application...

7 minutes read.

Copy elision in C++

The Copy Omission is another name for the Copy Elision. One of the several compiler optimization techniques is copy elision. It prevents items from being copied inadvertently. This Copy Elision approach...

3 minutes read.

wcscpy(), wcslen(), wcscmp() Functions in C++

There are many built-in functions in C++ programming language which differentiate it from C programming language in most hardware-coded languages. We will now closely look into the applications of three...

4 minutes read.

Sum of all elements between k1’th and k2’th Smallest Elements

In this tutorial, we will look at how to determine sum of all given elements between two given indexes’ smallest elements. Assuming an array of integers and two numbers, k1...

2 minutes read.

Print Table Using 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.

Inheritance Program in C++

What is Inheritance? Inheritance is the ability of a class to inherit traits and properties from another class. One of the most crucial aspects of Object-Oriented Programming is inheritance. The ability or...

9 minutes read.

C++ if-else

C++ if else Control Statement if else control statement in C++ is used to control the program flow in a two-way direction. When condition returns a true value, then the program executes if condition block otherwise...

1 minute read.

Differences between Local and Global Variable

Define Global Variable Global variables are those that may be accessible worldwide across a programme and are defined outside of any functions or blocks. It may be accessed by any function in...

3 minutes read.

Precision of floating point numbers Using these functions floor(), ceil(), trunc(), round() and setprecision() in C++

Precision of floating point numbers Using these functions floor(), ceil(), trunc(), round() and setprecision() in C++ 1/2 decimal equal is 0.555555555555555555555 .... An indefinite number of lengths will require the storage...

4 minutes read.