Operator Overloading on String Functions C++ Language
Write a program to perform Operator Overloading on String Functions using C++ Language . String Operations like
- Input String
- Concatenation String
- Reverse String
- Display String
- Equality String
- String Copy
All these Functions should be implemented by Operator Overloading on String Functions C++ Language .
Operator Overloading on String Functions Code
#include #include #include #include class sub; class overlod { char *p; int len; public: overlod() { len=0; p=new char[20]; } overlod(char *s) { len=strlen(s); p=new char[len+1]; } void input() { cout<<"\nENTER THE STRING"; cin>>p; } void operator<<(overlod s1) { cout<<s1.p; <br=""> getch(); } friend overlod operator+(overlod &s,overlod &t) { overlod temp; temp.len=s.len+t.len; temp.p=new char[temp.len+1]; strcpy(temp.p,s.p); strcat(temp.p,t.p); return temp; } int operator=(overlod s2) { int i; i=strcmp(p,s2.p); return i; } void operator==(overlod s3) { len=strlen(s3.p); p=new char[len+1]; strcpy(p,s3.p); } void operator>>(overlod s1) { strrev(s1.p); cout<<s1.p; <br=""> getch(); strrev(s1.p); } int operator -() { int i=0,j; j=strlen(p)-1; for(i=0;i<=j;i++,j--) { if((*(p+i))!=(*(p+j))) return 0; } return 1; } }; void main(void) { overlod s1,s2,s3; int ch,i; do { clrscr(); cout<<"\nSTRING OPERATION"<<"\n1.INPUT"<<"\n2.CONCATENATION"<<"\n3.REVERSE"<<"\n4.DISPLAY STRING"<<"\n5.EQUALITY"<<"\n6.STRING COPY"<<"\n7.PALLINDROME"<<"\n8.EXIT"; cout<<"\n ENTER UR CHOICE"; cin>>ch; clrscr; switch(ch) { case 1: cout<<"\ninput"; s1.input(); s2.input(); break; case 2: cout<<"\nCONCATENATION OF STRING1 & STRING2 IS:"; s1<<s1+s2; <br=""> break; case 3: cout<<"\nREVERSE STRING OF STRING IS:"; s2>>s1; break; case 4: cout<<"\nDISPLAYING STRING"; s2<<s1; <br=""> break; case 5: i=(s1=s2); if(i!=0) cout<<"NOT EQUAL"; else cout<<"EQUAL"; getch(); break; case 6: cout<<"\nCOPIED STRING IS:"; s3==s1; s2<<s3; <br=""> break; case 7: i=-s1; if(i!=0) cout<<"\nSTRING IS PALINDROME"; else cout<<"\nSTRING IS NOT PALINDROME"; getch(); break; case 8:break; default: cout<<"\nINVALID CHOICE"; } }while(ch!=8); }
Leave a Reply