Gice

Technology and General Blog

The way to merge two or a lot more strings is called string concatenation. It is a really prevalent endeavor for any programming language. Some programming languages use a certain operator, some programming languages use the constructed-in perform, and some programming languages use both of those operator and crafted-in perform to merge string values. The string concatenation can be done by working with the ‘+’ operator and diverse forms of crafted-in functions in C++. The employs of the ‘+’ operator and different designed-in capabilities to combine strings in C++ have been explained in this tutorial.

Pre-requisite:

Right before examining the examples of this tutorial, you have to verify the g++ compiler is installed or not in the method. If you are working with Visible Studio Code, then set up the needed extensions to compile the C++ supply code to build the executable code. Below, the Visible Studio Code software has been utilised to compile and execute the C++ code.

Employing ‘+’ operator for string concatenation:

Applying the ‘+’ operator is the most straightforward way to merge two strings. Develop a C++ file with the adhering to code for combining two strings by employing the ‘+’ operator. Two vacant string variables are declared at the commencing of the code. These variables have been used to store the input taken from the consumer. Subsequent, two values have combined to make the new string that has been printed afterwards.

//Contain important libraries

#include

#include

int most important()

       //Delare two string variables

       std::string strData1=“”, strData2=“”

       //Just take two string values from the consumer

       std::cout<<“Enter the first string:”

       std::cin>>strData1

       std::cout<<“Enter the second string:”

       std::cin>>strData2

       //Merge the string values

       std::string combine = strData1 + strData2

       //Print the concatenated string price

       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 necessary libraries

#include

#include things like

using namespace std

int principal()

       //Delare two string variables

       char chrData1[100], chrData2[100]

       //Declare a string variable

       string strData1, strData2

       //Acquire the initially string facts and change it into character array

       cout<<“Enter the first string:”

       cin>>strData1

       strcpy(chrData1, strData1.c_str())

       //Take the second string facts and change it into character array

       cout<<“Enter the second string:”

       cin>>strData2

       strcpy(chrData2, strData2.c_str())

       //Depend the complete ingredient of the initial character array

       int len = strlen(chrData1)

       /*

       Iterate the loop to insert all aspects of

       the 2nd character array

       to the initially character array

       */

       for(int i= 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:

strcat(char *array1, char *array2)

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 necessary libraries

#include

#incorporate

utilizing namespace std

int key()

       //Delare two string variables

       char chrData1[100], chrData2[100]

       //Take the first string facts

       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:

string & string::append ( const string& str )

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 necessary libraries

#include

making use of namespace std

int primary ()

       //Declare the output variable

       string outstr

       //Assign a few 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.

Leave a Reply

Your email address will not be published. Required fields are marked *