The way to merge two or more strings is called string concatenation. It is a very common task for any programming language. Some programming languages use a specific operator, some programming languages use the built-in function, and some programming languages use both operator and built-in function to combine string values. The string concatenation can be done by using the ‘+’ operator and different types of built-in functions in C++. The uses of the ‘+’ operator and different built-in functions to combine strings in C++ have been explained in this tutorial.
Pre-requisite:
Before checking the examples of this tutorial, you have to check the g++ compiler is installed or not in the system. If you are using Visual Studio Code, then install the necessary extensions to compile the C++ source code to create the executable code. Here, the Visual Studio Code application has been used to compile and execute the C++ code.
Using ‘+’ operator for string concatenation:
Using the ‘+’ operator is the simplest way to combine two strings. Create a C++ file with the following code for combining two strings by using the ‘+’ operator. Two empty string variables are declared at the beginning of the code. These variables have been used to store the input taken from the user. Next, two values have combined to generate the new string that has been printed later.
#include <string>
#include <iostream>
int main()
{
//Delare two string variables
std::string strData1="", strData2="";
//Take two string values from the user
std::cout<<"Enter the first string:";
std::cin>>strData1;
std::cout<<"Enter the second string:";
std::cin>>strData2;
//Combine the string values
std::string combine = strData1 + strData2;
//Print the concatenated string value
std::cout << "The Concatenated string is:" << combine << '\n';
return 0;
}
Output:
If the user types ‘Hello’ and ‘World’ as input values after executing the code, then the following output will appear.
Using a loop for string concatenation:
Using a loop is another way to combine the string values of the char array. Create a C++ file with the following code to check the use of the ‘for’ loop for combining the string values stored in two char array variables. Two string variables and two char arrays of 100 elements have been declared in the code. The input values will be stored in the string variables, and the strcpy() function will store the string values into the char arrays. Next, the length of the first char array has counted and used in the ‘for’ loop to append the content of the second char array at the end of the first char array. The values of the first char array will be printed after combining all elements of the second char array.
#include<iostream>
#include <cstring>
using namespace std;
int main()
{
//Delare two string variables
char chrData1[100], chrData2[100];
//Declare a string variable
string strData1, strData2;
//Take the first string data and convert it into character array
cout<<"Enter the first string:";
cin>>strData1;
strcpy(chrData1, strData1.c_str());
//Take the second string data and convert it into character array
cout<<"Enter the second string:";
cin>>strData2;
strcpy(chrData2, strData2.c_str());
//Count the total element of the first character array
int len = strlen(chrData1);
/*
Iterate the loop to insert all elements of
the second character array
to the first character array
*/
for(int i=0; i < sizeof(chrData2); i++)
{
chrData1[len] = chrData2[i];
len++;
}
//Print the conactenated outpyt
cout << "The concatenated string is :" << chrData1 << endl;
return 0;
}
Output:
If the user types ‘Linux’ and ‘Hint’ as input values after executing the code, then the following output will appear.
Using strcat() function for string concatenation:
The strcat() is a built-in function to concatenate two string values. It takes two char arrays as argument values and the concatenated value of the arrays. The syntax of this function has given below.
Syntax:
Create a C++ file with the following code to concatenate the value of two char arrays using the strcat() function. Two char arrays have been declared in the code to store the input values taken from the user. Next, the strcat() function has used to print the concatenated value of two char arrays.
#include <cstring>
#include <iostream>
using namespace std;
int main()
{
//Delare two string variables
char chrData1[100], chrData2[100];
//Take the first string data
cout<<"Enter the first string:";
cin.getline(chrData1, 100);
//Take the second string data
cout<<"Enter the second string:";
cin.getline(chrData2, 100);
//Print the concatenated string
cout << "The concatenated String is:" << strcat(chrData1, chrData2) << endl;
cout << chrData1;
return 0;
}
Output:
If the user types ‘C++’ and ‘Programming’ as input values after executing the code, then the following output will appear.
Using append() function for string concatenation:
The append() is another built-in function that returns the concatenated string of two string values and takes a string in the argument. The syntax of this function is given below.
Syntax:
Create a C++ file with the following code to concatenate two string values using the append() function. Four-string variables have been declared in the code. One string variable will store the concatenated string value, and three string values have been stored in the other three variables that have been concatenated using the append() function. The append() function has been called three times in the code to concatenate the values of three string variables and append the values into the outstr variable that has been printed later.
#include <iostream>
using namespace std;
int main ()
{
//Declare the output variable
string outstr;
//Assign three string values
string string1 = "I";
string string2 = " Like";
string string3 = " Programming.";
//Append the three string to the output variable
outstr.append(string1);
outstr.append(string2);
outstr.append(string3);
//Print the concatenated output
cout << "The concatenated String is:" << outstr << '\n';
return 0;
}
Output:
The following output will appear after executing the above code.
Conclusion:
Different ways to concatenate strings in C++ have been explained in this tutorial by using four simple examples. The string values can be concatenated using a built-in function and without using a built-in function in C++. The C++ programmer can use any of the ways shown here to concatenate string values.
from Linux Hint https://ift.tt/3BFFPhJ
0 Comments