Advertisement
Aniket_Goku

p4

Nov 4th, 2020
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.34 KB | None | 0 0
  1. //4. Create a class String and overload operator +, ==, - operator to perform string
  2. //concatenation and comparison and string reverse.
  3. #include<iostream>
  4. #include<string.h>
  5. using namespace std;
  6. class Str
  7. {
  8.     char s1[20];
  9.     public:
  10.        
  11.         void get_s()
  12.         {
  13.             cout<<"\n Enter String:";
  14.             cin>>s1;
  15.         }
  16.         void put_s()
  17.         {
  18.             cout<<"String=> "<<s1;
  19.         }
  20.         void operator +(Str a1)
  21.         {
  22.             cout<<s1;
  23.             cout<<a1.s1;
  24.            
  25.         }
  26.         void operator ==(Str a)
  27.         {
  28.             if((strcmp(s1,a.s1))==0)
  29.             {
  30.                 cout<<"\n Both String Are Same";
  31.             }
  32.             else
  33.             {
  34.                 cout<<"\n Both String Are Different";
  35.             }
  36.         }
  37.         void operator -()
  38.         {
  39.             cout<<strrev(s1);
  40.         }
  41. };
  42. int main()
  43. {
  44.     Str s,a;
  45.     int ch;
  46.     do
  47.     {
  48.         cout<<"\n =============> Menu <================= \n";
  49.         cout<<"\n 1.Concate two string ";
  50.         cout<<"\n 2.Compare two string";
  51.         cout<<"\n 3.reverse String";
  52.         cout<<"\nEnter Your Choice";
  53.         cin>>ch;
  54.         switch(ch)
  55.         {
  56.             case 1:
  57.                 cout<<"\n Enter 1st String:";
  58.                 s.get_s();
  59.                 cout<<"\n Enter 2nd String:";
  60.                 a.get_s();
  61.                 s+a;
  62.                 break;
  63.                
  64.             case 2:
  65.                 cout<<"\n Enter 1st String:";
  66.                 s.get_s();
  67.                 cout<<"\n Enter 2nd String:";
  68.                 a.get_s();
  69.                 s==a;
  70.                 break;
  71.                
  72.             case 3:
  73.                 cout<<"\n Enter 1st String:";
  74.                 s.get_s();
  75.                 -s;
  76.                 break;
  77.            
  78.         }
  79.        
  80.        
  81.     }while(ch>=1 && ch<=3);
  82.     return 0;
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement