Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Checking two Strings equal or not and Concatenate two strings using Class in C++



In this C++ program we will Create a class ‘STRING’ which has one data member ‘str’. The class can perform concatenation of two string by ‘+’ operator overloading and check given two string are equal or not by ‘==’ operator overloading. Strings will be given by the user.color>

input:

Two Strings.

output:

Either the strings will be equal to each other or not and after that they will be concatenated.

CODE---->



#include
#include
using namespace std;
class STRING
{
private:
char str[100];
public:
void getString()
{
gets(str);
}
void display()
{
puts(str);
}
STRING operator+(STRING X)
{
STRING s;
strcpy(s.str,str);
strcat(s.str,X.str);
return(s);
}
int operator==(STRING X)
{
if(strcmp(str,X.str)==0)
return 1;
else
return 0;
}
};
int main()
{
STRING s1,s2,s3;
cout"Enter 1st String: ";
s1.getString();
cout"Enter 2nd String: ";
s2.getString();
cout"\n-----------------------\n";
cout"1st String is: ";
s1.display();
cout"2nd String is: ";
s2.display();
cout"\n-----------OUTPUT--------------\n";
if(s1==s2)
cout"**String are equal**";
else
cout"**String are not equal**";
cout"\nConcatenation of given two string is: ";
s3=s1+s2;
s3.display();
}


Download the C-Program file of this Program.

Don't just read, run on your pc !!!color>


RESULT :color>



Enter 1st String: You are the
Enter 2nd String: best programmer.

-----------------------
1st String is: You are the
2nd String is: best programmer.

-----------OUTPUT--------------
**String are not equal**
Concatenation of given two string is: You are the best programmer.

--------------------------------
Process exited after 72.06 seconds with return value 0
Press any key to continue . . .


Images for better understanding : color>




This post first appeared on ProgramJoy.blogspot.com, please read the originial post: here

Share the post

Checking two Strings equal or not and Concatenate two strings using Class in C++

×

Subscribe to Programjoy.blogspot.com

Get updates delivered right to your inbox!

Thank you for your subscription

×