/* String Operation By Overloading operator */
#include<string.h>
#include<iostream>
using namespace std;
class str
{
char *p;
int len;
public:
str()
{
len=0;
p=0;
}
str(const char *s);
str(const str &s);
friend str operator+(const str &s,const str &t);
friend int operator<(const str &s,const str &t);
friend int operator>(const str &s,const str &t);
friend int operator==(const str &s,const str &t);
friend void show(const str &s);
};
str::str(const char *s)
{
len=strlen(s);
p=new char[len+1];
strcpy(p,s);
}
str::str(const str &s)
{
len=s.len;
p=new char[len+1];
strcpy(p,s.p);
}
int operator<(const str&s,const str &t)
{
int m=strlen(s.p);
int n=strlen(t.p);
if(m<n)
return(1);
else
return(0);
}
int operator>(const str &s,const str &t)
{
int m=strlen(s.p);
int n=strlen(t.p);
if(m>n)
return(1);
else
return(0);
}
int operator==(const str &s,const str &t)
{
int m=strlen(s.p);
int n=strlen(t.p);
if(m==n)
return(1);
else
return(0);
}
void show(const str &s)
{
cout<<s.p;
}
str operator+(const str &s,const str &t)
{
str temp;
temp.len=s.len+t.len;
temp.p=new char[temp.len+1];
strcpy(temp.p,s.p);
strcat(temp.p,t.p);
return(temp);
}
int main()
{
str t1,t2,t3;
int ch;
char s1[100],s2[100];
cout<<"enter strings s1,s2\n";
cin>>s1>>s2;
t1=s1;
t2=s2;
cout<<"first string =";
show(t1);
cout<<"\n";
cout<<"second string =";
show(t2);
cout<<"\n";
cout<<"1.comparision \n2.concatenation \n3.exit\n";
do
{
cout<<"enter your choice:";
cin>>ch;
switch(ch)
{
case 1:
if(t1<t2)
{
show(t1);
cout<<" smaller than ";
show(t2);
cout<<endl;
}
else if(t1>t2)
{
show(t1);
cout<<" greater than ";
show(t2);
cout<<endl;
}
else
{
show(t1);
cout<<" equal to ";
show(t2);
cout<<endl;
}
break;
case 2:
t3=t1+t2;
cout<<"concatenation of two strings\n";
show(t3);
cout<<endl;
break;
case 3:
exit(0);
default:
cout<<"invalid choice\n";
}
}
while(1);
return(0);
}
I KRISHNAPRASAD PURSUING MY B.TECH IN C.R.REDDY COLLEGE OF ENGINEERING TRADE COMPUTER SCIENCES AND ENGINEERING. I'M HAPPY TO PRESENT ALL TYPE OF PROGRAMS. I HOPE U UTILISE THIS BLOG AND ENJOY YOUR PROGRAMMING. THESE PROGRAMS ARE BEST COMPILED IN TURBO C++ COMPILERS AND SUN JAVA COMPILERS
Thursday, November 25, 2010
STRING OPERATIONS C++
Subscribe to:
Post Comments (Atom)

No comments:
Post a Comment