String Manipulation Using Operator Overloading

Home >> Sem 2 >>String Manipulation Using Operator Overloading

#include<string>
#include<iostream>

using namespace std;

class string_manip
{
public:
        char str1[30];
        
        string_manip()
        {
                strcpy(str1,"");
        }
        void getstring();
        void putstring();

        bool operator ==(char *mm)
        {
                if(strcmp(str1,mm)==0)
                        return true;
                else 
                        return false;
        }

        bool operator >(char *mm)
        {
                if(strcmp(str1,mm)>0)
                        return true;
                else 
                        return false;
        }
        char *operator +(char *sc)
        {
                return strcat(str1,sc);
        }

};

void string_manip :: getstring()
{
        cout<<endl<<"Enter your string <Type end o STOP> : ";
        cin>>str1;
}

void string_manip ::putstring()
{
        cout<<"your string is : "<<str1<<endl;
}

void main()
{
        string_manip st1;
        char MyStr[30];
        char *StrCnt;
        while(true)
        {
                st1.getstring();
                if(st1=="end")
                        break;

                cout<<endl<<"Enter Secind String : ";
                cin>>MyStr;

                if(st1 == MyStr)
                        cout<<endl<<st1.str1<<" = "<<MyStr<<endl<<endl;
                else if(st1 > MyStr)
                        cout<<endl<<st1.str1<<" > "<<MyStr<<endl<<endl;
                else
                        cout<<endl<<st1.str1<<" < "<<MyStr<<endl<<endl;
                
                StrCnt = new char [30];
                StrCnt = st1 + MyStr;
                cout<<"Concatination : "<<StrCnt;
        }
}