Write a program to compare two string using equal to operator.

#include<iostream.h>
#include<conio.h>
#include<string.h>
class string
{
    private:
    char str[40];
    public:
    void show()
    {
       cout<<str;
    }
    void getdata()
    {
      cin>>str;
    }
    friend int operator ==(string,string );
};
int operator ==(string t1,string t2)
{
if(strcmp(t1.str,t2.str)==0)
   return 1;
   else
     return 0;
}
void main()
{
   clrscr();
   string s1,s2,s3;
   cout<<endl<<"\nEnter the first string:";
   s1.getdata();
   cout<<endl<<"\nEnter the second string:";
   s2.getdata();
   cout<<endl<<"String 1:";
   s1.show();
   cout<<endl<<"string 2:";
   s2.show();
   if(s1==s2)
      cout<<endl<<"Strings are equal";
    else
       cout<<endl<<"String are unequal";
   getch();
}

No comments:

Post a Comment