Prashant | Wed, 29 Jul, 2020 | 105
Concatenation of strings in C++ can be performed using following methods:
Here we use a predefined function strcat() from standard string library. We have to use the header file <string.h> to implement this function.
Syntax
char * strcat(char * destination, const char * source)
Example
#include<iostream>
#include<string.h>
using namespace std;
int main()
{
//string 1
char src[100]="Selfcode ";
//string 2
char des[100]="is best site";
//Concatenating the string
strcat(src, des);
//printing
cout<<src<<endl;
return 0;
}
Output
Selfcode is best site.
In this we concatenate two different string without using concatenation function strcat().
Example
#include<iostream>
#include<string.h>
using namespace std;
int main()
{
//declare strings
char str1[100];
char str2[100];
//input first string
cout<<"Enter first string : "<<endl;
cin.getline(str1,100);
//input second string
cout<<"Enter second string : "<<endl;
cin.getline(str2,100);
//variable as loop counnters
int i, j;
//keep first string same
for(i=0; str1[i] != '\0'; i++)
{
;
}
//copy the characters of string 2 in string1
for(j=0; str2[j] != '\0'; j++,i++)
{
str1[i]=str2[j];
}
//insert NULL
str1[i]='\0';
//printing
cout<<str1<<endl;
return 0;
}
Output
Enter first string : SelfCode Enter second string : is best website. SelfCode is best website.
Here in this section we use the concatenation operator to concatenate two different strings.
Example
#include<iostream>
#include<string.h>
using namespace std;
int main()
{
//declare string objects
string str1;
string str2;
//input first string
cout<<"Enter first string : "<<endl;
cin>>str1;
//input second string
cout<<"Enter second string : "<<endl;
cin>>str2;
//declare another object, that will store the result
string str3;
//concatenate the string using "+"
str3 = str1 + str2;
//print the result
cout<<str3<<endl;
return 0;
}
Output
Enter first string : Self Enter second string : Code SelfCode
Xunfasewquc Wed, 07 Apr, 2021
http://slkjfdf.net/ - Eagoxefug <a href="http://slkjfdf.net/">Agabaneif</a> psa.pssx.selfcode.in.vzq.zx http://slkjfdf.net/
Eyatupicudooo Wed, 07 Apr, 2021
http://slkjfdf.net/ - Eromoam <a href="http://slkjfdf.net/">Adirizedo</a> qwc.asmx.selfcode.in.zvq.cu http://slkjfdf.net/