#include<iostream.h>
#include<conio.h>
#include<string.h>
class string
{
char *p;
int len;
public:
string(){len=0;p=0;}
string(const char *s);
int operator<(const string &);
int operator!=(const string &);
void show();
};
string::string(const char *s)
{
len=strlen(s);
p=new char[len+1];
strcpy(p,s);
}
void string ::show()
{
cout<<p;
}
int string :: operator<(const string &s2)
{
if(len==s2.len)
{
cout<<"\n\nTwo strings are having equal length";
return -1;
}
else
{
if (len<s2.len)
return(1);
else
return(0);
}
}

int string :: operator!=(const string &s2)
{
if(strcmp(p,s2.p))
return(1);
else
return(0);
}
int main()
{
clrscr();
char str1[20];
char str2[20];
cout<<"\n\nEnter First String : ";
cin>>str1;
string s1(str1);
cout<<"\n\nEnter Second String : ";
cin>>str2;
string s2(str2);
cout<<"\n\nFirst String : ";
s1.show();
cout<<"\n\nSecond String : ";
s2.show();
cout<<"\n\n";
int i=s1<s2;
if(i==1)
{
s1.show();
cout<<" is smaller than ";
s2.show();
cout<<"\n";
}
if(i==0)
{
s2.show();
cout<<" is smaller than ";
s1.show();
cout<<"\n";
}
if(s1!=s2)
{
cout<<"\n\nTwo strings are not equal";
}
else
{
cout<<"\n\nTwo strings are equal";
}
getch();
return 0;
}

2 Comments

Post a Comment

Previous Post Next Post