CPP Program concatenate two strings

In this program, we will discuss how to concatenate the two strings in the cpp programming language. Two methods of concatenation are discussed in the following.

 By using a string object :

If you use a string object to deal with the strings in cpp then this method is useful for you.

Code:

#include <iostream>

using namespace std;


int main()

{

    string str1, str2, sum;


    cout << "Enter string str1  ";

    getline (cin, str1);

//same for second string

    cout << "Enter string str2 ";

    getline (cin, str2);


    sum = str1 + str2;


    cout << "Result of two string  = "<< sum;


    return 0;

}

Output:
Enter string str1  cpp is best programming
Enter string str2 language
Result of two string  = cpp is best programming language


By using c style string:

if you use c style to deal with the string in the cpp then this method is helpful for you.

Code:

#include <iostream>

#include <cstring>

using namespace std;


int main()

{

    char str1[50], str2[50];


    cout << "Enter string s1 ";

    cin.getline(str1, 50);


    cout << "Enter string s2 ";

    cin.getline(str2, 50);


    strcat(str1, str2); 


    cout << "result = " << str1 << endl;

    


    return 0;

}


Output:
Enter string s1 cpp is good programming
Enter string s2  language
result = cpp is good programming language

Comments

Popular posts from this blog

Diferrence between flutter and react native

Flutter Widgets

Flutter layout