26.8.15

Create a 'STRING' class which overloads ‘ = = ' operator

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;

}




*******************************OUTPUT 1********************************

Enter the first string : cpc

Enter the second string : cs

Strings are not equal

******************************OUTPUT 2**********************************

Enter the first string : CPC

Enter the second string : CPC

Strings are equal


No comments:

Post a Comment

Comment Here