Sunday 5 August 2012

How to concatenate and compare two strings in C++

Program to concatenate and compare two string using inbuilt string class



#include<iostream>

#include<conio.h>

#include<string>

using namespace std;

int main()
{
    string first_string, second_string, result_string;

    cout << "\n\n\t\tConcatenate and Compare 2 Strings";

    cout << "\n\n First String   ->  ";

    cin >> first_string;

    cout << "\n\n Second String  ->  ";

    cin >> second_string;

    result_string = first_string + " " + second_string;

    cout << "\n\n String after Concatenation -> " << result_string;

    if (first_string.compare(second_string) == 0)
    {
      cout << "\n\n Strings are Equal/Same (Comparison is case sensitive).";
    }
    else
    {
      cout << "\n\n Strings are not Equal/Same (Comparison is case sensitive).";
    }
      
    getch();

    return 0;

}


Check out the link below for extensive detail on String manipulation in C++ -

No comments:

Post a Comment