Concatenate Two Strings and Literals in C++.

Concatenation is a process of linking two more strings together in a chain-like structure to form a bigger string. There are multiple ways of concatenating strings and here we are going to discuss all of them one by one with C++ example code.


In C++, we work with two different kinds of strings one is a C-type string which is also known as Character Array and another is a String Class and we have different methods to concatenate these strings. If you are not familiar with the difference between Character Array and String Class then you can check out our post Character Array Vs String Class in C++


Concatenate String Class Objects.

Using + operator: 

We can concatenate two strings easily just by using the simple '+' operator between two strings and it will return us a single concatenated string. 


C++ Example Code:

//C++ example program Concatenation two string
#include<iostream>
#include<string>
using namespace std;

int main(){
  
  string leftstr = "Welcome to ";
  string rightstr = "AlgoLesson";

  //concatenation
  string resultstr = leftstr + rightstr;
  cout<<resultstr<<endl;
  //leftstr = leftstr + rightstr
  leftstr += rightstr;
  cout<<leftstr<<endl;

  return 0;
}
Output:
Welcome to AlgoLesson
Welcome to AlgoLesson

In the above code, we have concatenated two string variables together using the plus operator (+) but when we try to concatenate a string with literals then we need to make sure that at least one operand must be of string type. Let's understand this point with the below example.
string str1 = "Hello";
string str2 = "World";
//OK concatenate successfully
string str3 = str1 + " AlgoLesson " + str2;

//Error - cannot add two string literals
string str4 = "Wrong" + "Concatenation";

//OK - Code execute from left to right 
string str5 = str1 + " Wrong" + " Concatenation";

//Error - One side of + operator must be a string type.
string str5 = "Wrong" + " Concatenation " + str1;

From the above example, we can understand that we cannot directly concatenate two string literals using the + operator. There must be a string one-string type operand at any one side of the + operator.  

Using append() string function:

We have one append() string member function that is also used to add the second string at the end of the first string and store the concatenated string into the first string. 

C++ Example Code:
//C++ program Concatenation two string using append()
#include<iostream>
#include<string>
using namespace std;

int main(){

  string first = "Hello";
  string second = "World";

  first.append(second);

  cout<<first<<endl;
  
  return 0;
}
Output:
HelloWorld

Concatenation C-type string (Character Array).

In C-type string, we have strcat() function that is available inside <string> header file in C++ programming language that appends one string at the end of another string. This function takes two arguments let's say str1 and str2 and then it copies all the character of str2 and add them at the end of str1.

syntax: strcat(str1, str2);

C++ Example Code:
//C++ program Concatenation C-type string
#include<iostream>
#include<cstring>
using namespace std;

int main(){

  char str1[10] = "Learning";
  char str2[10] = "Coding";

  strcat(str1, str2);
  cout<<str1<<endl;

  return 0;
}
Output:
LearningCoding
Note: C-type string does not support the use of operators on string that's one of the reason we cannot use + operator to concatenate two C-type or Character type string in C++. (alert-success)

Next:

⚡ Please share your valuable feedback and suggestion in the comment section below or you can send us an email on our offical email id ✉ algolesson@gmail.com. You can also support our work by buying a cup of coffee ☕ for us.

Similar Posts

No comments:

Post a Comment


CLOSE ADS
CLOSE ADS