String Operator Overloading in C++ Example
This program is a combination of various important string operations with the help of operator overloading. These are frequently asked questions in papers.
This program uses the standard C++ library “CString” for all of these string functions.
In this program we will perform the following operations on the strings with the help of operator overloading;
- + String concatenation
- = String compare
- == String copy
- / Finding substring
- << Displaying the string from the object of class T4Tutorias
- >> Reversing the string
Example of string Equality
Enter the 1st string: Welcome
Enter the end string: Welcome2
both strings are not equal
***********************
Enter the 1st string: T4T
Enter the end string: T4T
both strings are equal
Example of string Copy
Enter the 1st string: Welcome
the copies string is: Welcome
Example of string Concat
Enter the 1st string: Welcome
Enter the end string: to our site
After concatenation, the string will be: Welcome to our site
Example of string Reverse
Enter the 1st string: Welcome
The reverse string is: emoclew
Example of string palindrome
Enter the string: Welcome
Welcome is a string but not a palindrome
Enter the string: abc
abc is a string and a palindrome
Example of substring
Enter the 1st string: Welcome
Enter the 2nd string: come
come is a subset of Welcome.
Program
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 |
/******************************************************* String operations using operator overloading in C++ *******************************************************/ #include<iostream> #include<cstring> using namespace std; //class definition class T4Tutorials{ private: //character array to denote string char str[30]; public: //declarating the functions void input(); void show(); void operator== (T4Tutorials formal_parameter); int operator= (T4Tutorials formal_parameter); void operator+ (T4Tutorials formal_parameter); void operator<< (T4Tutorials formal_parameter); void operator>> (T4Tutorials formal_parameter); int operator/ (T4Tutorials formal_parameter); void palindrome(); }; // Function definitions void T4Tutorials::input() { cout<<"\nPlease Enter the string : "; cin>>str; } //function definition for show() void T4Tutorials::show() { cout<<"\n"<<str; } //Function definition for operator "==" void T4Tutorials::operator== (T4Tutorials formal_parameter) { strcpy(formal_parameter.str,str); cout<<"\n\tCopied String is : "<<formal_parameter.str; } //Function definition for operator "=" int T4Tutorials::operator= (T4Tutorials formal_parameter) { if(strcmp(str,formal_parameter.str)==0) return 1; //strings are equal return 0; //strings are not equal } //Function definition for operator "+" void T4Tutorials::operator+ (T4Tutorials formal_parameter) { strcat(str,formal_parameter.str); cout<<"\n\t--String After Concat is : "<<str; } //Function definition for operator "<<" void T4Tutorials::operator<< (T4Tutorials formal_parameter) { cout<<"\n\t--The string you entered is :"<<formal_parameter.str; } //Function definition for operator ">>" void T4Tutorials::operator>> (T4Tutorials formal_parameter) { int i; cout<<"\n\t--The string after reversing is : "; for(i=strlen(formal_parameter.str);i>=0;i--) cout<<formal_parameter.str[i]; } // int T4Tutorials::operator/ (T4Tutorials formal_parameter) { int flag=0,k,i,j,len=strlen(str),len1=strlen(formal_parameter.str)-1; for(i=0;i<len;i++) { if(str[i]==formal_parameter.str[0]) { if(str[i+len1]==formal_parameter.str[len1]) { for(j=i,k=0;j<i+len1+1,k<len1;j++,k++) { if(str[j]==formal_parameter.str[k]) flag=1; else { flag=
0; break; } } } } } if(flag==0) return 0; //not a substring return 1; //it is a substring } //Function definition for palindrome() void T4Tutorials::palindrome() { int i,j,flag=0; for(i=0,j=strlen(str)-1;i<=strlen(str),j>=0;j--,i++) { if(str[i]!=str[j]) { flag=1; cout<<"\n\t****Not a palindrome****"; break; } else flag=0; } if(flag==0) cout<<"\n\t--Palindrome--"; } //start of main function int main() { int opt,c,opt1=1; //object declaration T4Tutorials a,b; while(opt1==1 && opt!=8) { //showing menu to the user cout<<"\nMain Menu\n\t1.Equality\n\t2.String Copy\n\t3.Concat"; cout<<"\n\t4.Show\n\t5.Reverse\n\t6.Palindrome\n\t7.Sub String"; cout<<"\n\t8.Exit\n"<<endl<<"Please Enter your choice:"; //taking user choice cin>>opt; //handling user choice switch(opt) { case 1: cout<<"\nEnter the 1st string-\n"; a.input(); cout<<"\nEnter the 2nd string-\n"; b.input(); c=a=b; if(c==1) cout<<"\n\t****Strings are Equal****\n"; else cout<<"\n\t****Strings are not Equal****\n"; break; case 2: a.input(); //using function overloading for copying string a==b; break; case 3: cout<<"\nEnter the 1st string-\n"; a.input(); cout<<"\nEnter the 2nd string-\n"; b.input(); //using function overloading for concatenation a+b; break; case 4: a.input(); b<<a; break; case 5: a.input(); //showing string using "<<" operator b>>a; break; case 6: a.input(); //check for palindrome a.palindrome(); break; case 7: cout<<"\nEnter the Main string-\n"; a.input(); cout<<"\nEnter the other string-\n"; b.input(); //using operator overloading for finding substring c=a/b; if(c==1) cout<<"\n\t****Substring****\n"; else cout<<"\n\t****Not a substring****\n"; break; case 8: return 0; default: cout<<"Invalid choice..try again\n"; } if(opt!=8){ cout<<"\n\n\tDo you want to continue(Press 1 to continue)"; cin>>opt1;} } return 0; } |
Output