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
Mukesh Rajput

Mukesh Rajput

I am a Computer Engineer, a small amount of the programming tips as it’s my hobby, I love to travel and meet people so little about travel, a fashion lover and love to eat food, I am investing a good time to keep the body fit so little about fitness also..

Post A Comment:

0 comments: