Easy methods to Concatenate String in C++
With a purpose to concatenate or mix two or extra strings in C++, it’s possible you’ll use other ways. These embrace:
- Utilizing + operator
- A predefined library perform append()
- Through the use of strcat() perform
The next part reveals examples for every of the above talked about methods.
Utilizing ‘+’ operator to mix strings in C++
Utilizing ‘+’ operator is only a matter of utilizing a + arithmetic operator that returns two or extra given strings mixed.
An instance of mixing two strings:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
|
#embrace <iostream>
#embrace <string>
utilizing namespace std;
int fundamental () {
string str1 = “C++ “;
string str2 = “Tutorials”;
string str_concat = str1 + str2;
cout <<“The concatenated String: “ << str_concat <<“nn”;
return 0;
} |
Output:
Concatenating three strings by ‘+’ operator instance
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
|
#embrace <iostream>
#embrace <string>
utilizing namespace std;
int fundamental () {
string str1 = “C++ “;
string str2 = “Concatenation “;
string str3 = “Tutorial”;
string str_concat = str1 + str2 + str3;
cout <<“The concatenated String: “ << str_concat <<“nn”;
return 0;
} |
Output:
You may see within the first instance, we mixed two strings and within the second C++ program, we mixed three strings by utilizing the ‘+’ operator.
Second means: utilizing append() perform instance
Following is the syntax of utilizing the append() perform to mix strings:
Str1.append(Str2);
Which means, the append perform takes a string as a parameter. This string is added to the top of one other string object.
The instance beneath reveals the append perform utilization:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
|
#embrace <iostream>
#embrace <string>
utilizing namespace std;
int fundamental () {
//Creating String objects
string str1 = “C++ “;
string str2 = “Append Tutorial”;
//Utilizing append perform to concat two strings
string str_append = str1.append(str2);
//Displaying concatenated string
cout <<“The concatenated String: “ << str_append <<“nn”;
return 0;
} |
Output:
Third means of concatenation – Utilizing strcat() perform
You may as well use strcat() to concatenate two strings simply. Following is the syntax:
char* strcat( char* dest, const char* src );
- It takes two parameters (char arrays)
- The src parameter is appended to the dest
- This perform returns the dest. pointer to the vacation spot string.
- Outlined within the <cstring> header file
See a C++ program for strcat() utilization:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
|
#embrace <cstring>
#embrace <iostream>
utilizing namespace std;
int fundamental()
{
//Creating char arrays to make use of in strcat()
char dest[30] = “That is C++ “;
char src[30] = “concatenation tutorial!”;
//Utilizing stringcat perform to concat
strcat(dest, src);
//Displaying finish end result
cout <<“The concatenated string: “ << dest <<“nn”;
return 0; } |
Output: