String Operations using Operator Overloading (C++)
April 24, 2016
Categorised in: C++ Codes, OOMP Codes
/*write a program to perform string operations
= equality
== string copy
+ concatenation
>> reverse
<< display
palindrome function
function to find occurence of substring
use operator overloading*/
#include<iostream>
#include<string.h>
#include<iomanip>
using namespace std;
class strings
{
char str[100];
public:
void getdata()
{
cout<<"enter string\t";
cin>>str;
}
void showdata() {cout<<str<<endl;}
int operator ~();
int operator =(strings s2);
void operator ==(strings s2);
friend void operator +(strings s1,strings s2);
friend ostream &operator <<(ostream &output,strings &s1)
{
output<<s1.str;
return output;
}
void operator >>(strings &s1);
friend void operator *(strings s1);
friend void operator /(strings s1,strings s2);
};
//string length
int strings::operator ~()
{
int i=0;
while(str[i]!=’\0’)
i++;
return i;
}
//string compare
int strings::operator =(strings s2)
{
int i=0;
while(str[i]==s2.str[i] && i<~s2)
i++;
if(i==~s2)
return 1;
else
return 0;
}
//copy
void strings::operator ==(strings s2)
{
for(int i=0;i<=~s2;i++)
str[i]=s2.str[i];
}
//concat
void operator +(strings s1,strings s2)
{
int i,j,k=0;
strings s3;
for(i=0;i<~s1;i++)
{
s3.str[k]=s1.str[i];
k++;
}
for(j=0;j<~s2;j++)
{
s3.str[k]=s2.str[j];
k++;
}
cout<<s3.str<<endl;
}
//reverse
void strings:: operator >>(strings &s1)
{
int len=~s1;
for(int i=0;i<=len/2;i++)
{
char t=s1.str[i];
s1.str[i]=s1.str[len-i-1];
s1.str[len-i-1]=t;
}
}
//palindrome
void operator *(strings s1)
{
strings s3;
s3==s1;
s3>>s3;
if((s3=s1)==1)
cout<<"palindrome\n";
else
cout<<"not palindrome\n";
}
//find substring
void operator /(strings s1,strings s2)
{//s2=substring
int flag=0;
for(int i=0;i<~s1;i++)
{
if(s2.str[0]==s1.str[i])
{
flag++;
for(int j=1;j<=~s2;j++)
if(s2.str[j]==s1.str[i+j])
flag++;
}
}
if(flag==~s2)
cout<<"substring exists\n";
else
cout<<"not found\n";
}
int main()
{
strings s1,s2;
int choice;
char c;
cout<<setw(20)<<"STRING OPERATIONS"<<endl;
cout<<"1.accept and display string \n2.check for equality\n3. copy string\n 4.concatenate strings \n5.reverse string\n6.check if a string is a palindrome\n7.search for a substring occurence\n";
do
{
cout<<"enter choice\t";
cin>>choice;
if(choice==2)
{
cout<<"enter first string\n";
s1.getdata();
cout<<"enter second string\n";
s2.getdata();
if((s1=s2)==1)
cout<<"strings are same\n";
else cout<<"strings are different\n";
}
if(choice==3)
{
cout<<"enter string to copy\n";
s2.getdata();
s1==s2;
cout<<"contents copied to another string\n";
cout<<"contents of copy string is :\t";
cout<<s1<<endl;
}
if(choice==4)
{
cout<<"enter destination string:\n";
s1.getdata();
cout<<"enter source string:\n";
s2.getdata();
s1+s2;
}
if(choice==1)
{
s1.getdata();
cout<<"string is:\t";
cout<<s1<<endl;
cout<<~s1;
cout<<endl;
}
if(choice==5)
{
s1.getdata();
s1>>s1;
cout<<"string after reversing is\t";
cout<<endl;
cout<<s1<<endl;
}
if(choice==6)
{
s1.getdata();
*s1;
}
if(choice==7)
{
cout<<"enter main string\n";
s1.getdata();
cout<<"enter substring\n";
s2.getdata();
s1/s2;
}
cout<<"do you want to continue(y/n)?";
cin>>c;
}while(c==’y’);
return 0;
}
Pratik Kataria is currently learning Springboot and Hibernate.
Technologies known and worked on: C/C++, Java, Python, JavaScript, HTML, CSS, WordPress, Angular, Ionic, MongoDB, SQL and Android.
Softwares known and worked on: Adobe Photoshop, Adobe Illustrator and Adobe After Effects.