Code Competitions

Coding competition

Saturday, 15 June 2013

program to compare 2 strings using operator overloading

#include<iostream.h>
#include<string.h>
#include<stdio.h>
#include<conio.h>
class cat
{
    private:
    char *str,*str1,ch;
    int i;
    public:
    void getdata()
    {

        str=str1;
        i=0;
        cout<<"\nenter the string ";
        while((ch=getchar())!='\n')
        {
            *(str+i)=ch;
            i++;
        }
        *(str+i)=' ';
        *(str+(++i))=NULL;
    }
    void display()
    {
        cout<<"\nthe entered string is: ";
        //cout<<str;
        //str=str1;
        i=0;
        while(*(str+i)!='\0')
        {
            putchar(*(str+i));
            i++;
        }
    }
    cat operator+(cat c)
    {
        strcat(c.str,str);
        return(c);
    }
    void operator<(cat c2)
    {
        if(strcmp(str,c2.str))
        {
            cout<<"\nthe string "<<c2.str<<" is larger than "<<str;
        }
        else
        {
            cout<<"\nthe string"<<c2.str<<" is larger than "<<str;
        }
    }

};
void main()
{
    clrscr();
    cat c1,c2;
    c1.getdata();
    c2.getdata();
    c1.display();
    c2.display();
    cout<<"\nafter comparison of strings\n ";
    c1.operator<(c2);
    c2=c1.operator+(c2);
    cout<<"\nafter concatination :";
    c2.display();
    getch();
}











No comments:

Post a Comment