How to concatenate two strings in C++ ?

Written by

Juhi Kamdar

Concatenate of two strings in C++

Combining two strings is frequently used when using higher level programming. To perform such a task in C++, we can use one of the three methods given below:

  1. Appending characters of second string in the first string.
  2. Adding two strings using binary ‘+’ operator.
  3. Use of predefined strcat()
  4. Use of predefined strncat()

Method 1: Appending characters of the second string in the first string:

Logic:

We input the characters of the second string to the first string, one by one.

Algorithm:

  1. Take two strings as input.
  2. Initialize i as length of string1 -1
  3. Run a loop with characters of j
  4. Store characters of string2 in 1, and then, increment i.
  5. Terminate the string1 with ‘\0’
  6. Output the resultant string.

Code:

#include <iostream.h>
#include<conio.h>
#include<string.h>
void main()
{
	char str1[30]="blue";
	char str2[30] = "oceans";  
	int i=0,stop;
//to get the last index containing character 
do	{ 
	stop=i++;
}while(str1[i]!='\0');

i=stop+1;
//concate strings
for(int j = 0; str2[j] != '\0'; j++, i++)
	str1[i] = str2[j];	//copying chars of string2 in 1, one by one

str1[i] = '\0';    //to terminate resultant string
cout&lt;&lt;"\n Resultant string is: "&lt;&lt; str1;
getch();

}

Output:

Resultant string is: blueoceans

Method 2: Adding two strings using binary ‘+’ operator:

Logic:

We input the characters of the second string to the first string, one by one.

Algorithm:

  1. Take two strings as input.
  2. Initialize i as length of string1 -1
  3. Run a loop with characters of j
  4. Store characters of string2 in 1, and then, increment i.
  5. Terminate the string1 with ‘\0’
  6. Output the resultant string.

Code:

#include <iostream>
using namespace std;

int main() { string str1, str2, newstr; cout << "Enter string 1: "; getline (cin, str1); cout << "\n Enter string 2: "; getline (cin, str2); newstr = str1 + str2;//concatenation cout << "\n Concated String: "<< newstr; return 0; }

Output:

Enter string 1: Good
Enter string 2: Morning
Concated String: GoodMorning

 Method 3: Use of predefined strcat():

Logic:

In this method, we take two strings as input. And then using strcat() function, concatenates the second string with the first one. strcat() doesn’t return anything.

Algorithm:

  1. Take two strings as input.
  2. Use strcat(), with destination string and source strings as parameters.
  3. Output the resultant string.

Code:

#include <iostream.h>
#include <string.h> //contains strcat
#include<conio.h>
void main()
{
	string str1,str2;
    cout<<"Enter string 1";
    getline(cin,str1);
	cout<<"Enter String 2:\n";
	getline(cin,str2);
    strcat(string1, string2);  
	cout<<"Resultant string is: "<< str1;
   getch();
 }

Output:

Enter string 1:Tab

Enter String 2:les

Resultant String is:Tables

Method 4: Use of predefined strncat():

Logic:

This method takes two strings as input. It concates the second string with the first one. But, in this method, we may concat as many characters as we wish. It doesn’t return anything.

Syntax:

strcat(destinationString,sourceString,charactersToBeConcated )

Algorithm:

  1. Take two strings as input.
  2. Use strncat(), with destination string , source strings and an integer as parameters.
  3. Output the resultant string.

Code:

#include <iostream.h>
#include <string.h> //contains strncat
#include<conio.h>

void main() { string str1,str2; cout<<"Enter string 1"; getline(cin,str1); cout<<"Enter String 2:\n"; getline(cin,str2); strncat(str1, str2,3); //to concate 3 chars of second string cout<<"Resultant string is: "<< str1; getch(); }

Output:

Enter string 1:Aqua

Enter String 2:Manhood

Resultant String is:AquaMan

 

 

How to concatenate two strings in C++ ?