Write C++ program to concatenate two strings

 #include <iostream>

#include <string>

using namespace std;


int main()

{

    string str1, str2, result;


    cout << "Enter the first string: ";

    getline(cin, str1);


    cout << "Enter the second string: ";

    getline(cin, str2);


    result = str1 + str2;


    cout << "Concatenated string: " << result << endl;


    return 0;

}



In this program, we use the string data type to store the strings. We declare three string variables str1, str2, and result.

We then use the getline function to read the values of str1 and str2 from the user.

Next, we concatenate str1 and str2 using the + operator and store the result in result.

Finally, we display the concatenated string result using cout.

Post a Comment

0 Comments