payoffer

PayOffers.in

Tuesday, March 25, 2014

Program to perform String operations using operator overloading in C++

C++ code to perform string operations  using  operator overloading 
i. = Equality
ii. == String Copy
iii. + Concatenation
iv. << To display a string

Description :Operator Overloading : It is a specific case of polymorphism where different operators have different implementations depending on their arguments.


Code :
#include<iostream.h>
#include<stdio.h>
#include<conio.h>
#include<string.h>
#define MAX 50
class string
{
char str[MAX];
public:
friend void operator>>(istream &din,string &s1)
{
din>>s1.str;
}

friend void operator<<(ostream &dout,string &s1)
{
dout<<s1.str;
}
void operator+=(string s1);
void operator==(string s1);
void operator=(string s1);
void operator~();
void operator!();
void operator^(string s1);
string()
{
strcpy(str,” “);
}
string(char s[])
{
strcpy(str,s);
}
};
void string::operator+=(string s1)
{
if(strlen(str)+strlen(s1.str)<MAX)
{
strcat(str,s1.str);
cout<<”\n\nConcatinated string is: “<<str;
}
else
{
cout<<”\n\nSorry the array size is exceeded.”;
}
}
void string::operator==(string s1)
{
if(strcmp(str,s1.str)==0)
cout<<”\n\nStrings are equal.”;
else
cout<<”\n\nStrings are not equal.”;
}
void string::operator=(string s1)
{
strcpy(str,s1.str);
cout<<”\n\nCopy of string is: “<<str;
}
void string::operator~()
{
strrev(str);
cout<<”\n\nReverse of string is: “<<str;
}
void string::operator!()
{
string temp;
strcpy(temp.str,str);
strrev(str);
if(strcmp(temp.str,str))
cout<<”\n\nString is not palindrome.”;
else
cout<<”\n\nString is palindrome.”;
}
void string::operator^(string s1)
{
if(strstr(str,s1.str))
cout<<”\n\n”<<s1.str<<” is substring of “<<str<<”.”;
else
cout<<”\n\nSubstring is not present.”;
}
void main()
{
string s1,s2;
int choice;
char ans;
clrscr();
cout<<”\n\n **** STRING OPERATIONS ****”;
do
{
cout<<”\n\nÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ”;
cout<<”\n\n MENU:”;
cout<<”\n\n\t1.Concatination of string\n\n\t2.Equality\n\n\t3.Copy string\n\n\t4.Reverse of string\n\n\t5.Palindrome\n\n\t6.Occurance of substring”;
cout<<”\n\nÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ”;
cout<<”\n\nEnter your choice: “;
cin>>choice;
switch(choice)
{
case 1:
cout<<”\n\nEnter the first string: “;
cin>>s1;
cout<<”\n\nEnter the second string: “;
cin>>s2;
s1+=s2;
break;
case 2:
cout<<”\n\nEnter the first string: “;
cin>>s1;
cout<<”\n\nEnter the second string: “;
cin>>s2;
s1==s2;
break;
case 3:
cout<<”\n\nEnter the string: “;
cin>>s1;
s2=s1;
break;
case 4:
cout<<”\n\nEnter the string: “;
cin>>s1;
~s1;
break;
case 5:
cout<<”\n\nEnter the string: “;
cin>>s1;
!s1;
break;
case 6:
cout<<”\n\nEnter the string: “;
cin>>s1;
cout<<”\n\nEnter the string which you want to check for occurance: “;
cin>>s2;
s1^s2;
break;
default:
cout<<”\n\nPlease enter correct choice.”;
break;
}
cout<<”\n\n\nDo you want to continue?(y/n): “;
flushall();
cin>>ans;
}while(ans==’y’ || ans==’Y');
getch();
}

No comments:

Post a Comment