#include<iostream.h>
#include<conio.h>
#include<string.h>
class string
{
char name[20];
public:
void getdata();
#include<conio.h>
#include<string.h>
class string
{
char name[20];
public:
void getdata();
void operator+(string y); //conca
int operator==(string y); //equality
void operator=(string y); //copy
friend int operator*(string x,string y); //substr
friend int operator-(string x); //palindr
friend string operator>>(string y,string x); //reverse
friend ostream&operator<<(ostream &x,string &y); // display
};
void string::getdata()
{
cout<<"\nenter the string\t";
cin>>name;
}
void string:: operator+(string y) //conca
{
string p;
int i=0,j=0;
while(name[i]!=NULL)
{
p.name[i]=name[i];
i++;
}
while(y.name[j]!=NULL)
{
p.name[i]=y.name[j];
i++;
j++;
}
p.name[i]='\0';
cout<<p;
}
int string ::operator==(string y) //equality
{
int i=0;
while(name[i]!='\0')
{
if(y.name[i]!=name[i])
return(0);
// break;
i++;
}
return(1) ;
}
void string:: operator=(string y) //copy
{
int i=0;
while(y.name[i]!='\0')
{
name[i]=y.name[i];
i++;
}
name[i]=NULL;
}
int operator*(string x,string y) //substr
{
int i=0;
while(y.name[i]!='\0')
{
if(x.name[i]!=y.name[i])
return(0);
// break;
i++;
}
return(1) ;
}
int operator-(string x) //palindr
{
int i,j,l=0;
while(x.name[i]!='\0')
{
l++;
i++;
}
for(i=0,j=l-1;i<l,j>=0;i++,j--)
{
if(x.name[i]!=x.name[j])
{
i++;
return 0;
}
}
return(1);
}
string operator >>(string y,string x) //reverse
{
int i,l=0,j=0;
while(x.name[i]!='\0')
{
l++;
i++;
}
i=l-1;
while(i>=0)
{
y.name[j]=x.name[i];
i--;
j++;
}
y.name[j]='\0';
cout<<y;
return(y);
}
ostream&operator<<(ostream &x,string &y) // display
{
cout<<"\nentered string \t";
x<<y.name;
return(x);
}
int main()
{
string a,b,c;
int o;
int k=0;
char d;
clrscr();
do
{
cout<<"\nenter your choice\t";
cout<<"\n1:to concatenate two strings \t";
cout<<"\n2:to find equality of two string\t";
cout<<"\n3:to copy one string to other \t";
cout<<"\n4:to find substring \t";
cout<<"\n5:to palindrome \t";
cout<<"\n6:to reverse a string \t";
cin>>o;
switch(o)
{
case 1:
{
a.getdata();
b.getdata();
a+b;
}
break;
case 2:
{
a.getdata();
b.getdata();
k=a==b;
if(k==1)
cout<<"\nstrings are equal\t";
else
cout<<"\nstrings are not equal\t";
}
break;
case 3:
{
b.getdata();
a=b;
cout<<"\nstring copied\t";
cout<<a;
}
break;
case 4:
{
a.getdata();
b.getdata();
k=a*b;
if(k==1)
cout<<"\nstring b is a sub string of a\t";
else
cout<<"\nstrings are not equal\t";
}
break;
case 5:
{
a.getdata();
k=-a;
if(k==1)
cout<<"\nstring is palindrome\t";
else
cout<<"\nstring is not palindrome\t";
}
break;
case 6:
{
a.getdata();
c>>a;
}
break;
default:
cout<<"\nwrong choice\t";
break;
}
cout<<"\nif you want to continue please press .....y\t";
cin>>d;
}while(d=='y'||d=='Y');
return(0);
}
No comments:
Post a Comment