Create a 'STRING' class which overloads ‘ == ' operator to compare two STRING objects.
#include<iostream.h>
#include<conio.h>
#include<string.h>
class string
{
char *p;
int len;
public:
string()
{ }
string( char *s)
{
len=strlen(s);
p=new char[len+1];
strcpy(p,s);
}
friend int operator = =( string &s,string &t);
};
int operator = = ( string &s1,string &s2)
{
if(strcmp(s1.p,s2.p)= =0)
return(1);
else
return(0);
}
int main()
{
string s1,s2;
char str1[20],str2[20];
clrscr();
cout<<"\nEnter the first string : ";
cin>>str1;
cout<<"\nEnter the second string : ";
cin>>str2;
s1=str1;
s2=str2;
if(s1= =s2)
cout<<"\nStrings are equal";
else
cout<<"\nStrings are not equal";
getch();
return 0;
}
The output of the above program:
Enter the first string : cpc
Enter the second string : cs
Strings are not equal
Enter the first string : CPC
Enter the second string : CPC
Strings are equal
Thanks
Mukesh Rajput
Post A Comment:
0 comments: