Include:
iostream.h
conio.h
process.h
class string
{
public:
char *str;
void operator=(string);
void operator==(string);
void operator+(string);
void operator<=(string);
void operator>=(string);
friend ostream* operator<<(ostream*,string *s1);
friend istream& operator>>(istream&,string &s1);
};
// String Comparison
void string::operator=(string s2)
{
int i,j,flag=0;
for(i=0;s2.str[i]!='\0'||str[i]!='\0';i++)
{
if(s2.str[i]!=str[i])
{
flag=1;
}
}
if(flag==0)
cout<<"\nStrings are equal";
else
cout<<"\nString are not equal";
}
// String Copy
void string::operator==(string s2)
{
int i,j,c=0;
for(i=0;s2.str[i]!='\0';i++)
{
str[i]=s2.str[i];
c++;
}
cout<<"\nCopied string is:- ";
for(i=0;i<=c;i++)
cout<
}
// String Concatenatinon
void string::operator+(string s2)
{
int i,j;
for(i=0;str[i]!='\0';i++);
for(j=0;str[j]!='\0';j++)
{
str[i]=s2.str[j];
i++;
}
cout<<"\nConcated String is:- ";
for(i=0;str[i]!='\0';i++)
cout<
}
istream & operator >> (istream &in,string &s1)
{
for(int i=0;s1.str[i]!='\0';i++)
in>>s1.str[i];
return in;
}
// String Reverse
ostream& operator << (ostream &out,string &s1)
{
for(int i=0;s1.str[i]!='\0';i++);
for(int j=i;j>=0;j--)
out<
return out;
}
// String Palindrome Check
void string::operator<=(string s1)
{
int i,flag=1,j=0;
for(i=0;s1.str[i]!='\0';i++)
j++;
j--;
for(i=0;s1.str[i]!='\0';i++,j--)
if(s1.str[j]!=s1.str[i])
flag=0;
if(flag==1)
cout<<"\nString is Palindrome";
else
cout<<"\nString is not Palindrome";
}
// String SubString
void string::operator>=(string s2)
{
int flag=0,j=0,i;
for(i=0;str[i]!='\0';i++)
{
if(str[i]==s2.str[j])
j++;
else
j=0;
if(s2.str[j]=='\0')
{
cout<<"\nSub-String is Present";
flag=1;
}
}
if(flag==0)
cout<<"\nSubstring is Absent";
}
// Main Function
void main()
{
int ch;
string s1,s2;
char ans;
clrscr();
do
{
cout<<"\n---------Menu---------";
cout<<"\n\n1.Equality";
cout<<"\n2.Copy string";
cout<<"\n3.Concat ";
cout<<"\n4.Reverse";
cout<<"\n5.Palindrom";
cout<<"\n6.Substring ";
cout<<"\n7:Exit";
cout<<"\n\nEnter your Choice : ";
cin>>ch;
switch(ch)
{
case 1: cout<<"\nEnter String 1 For Comparison: ";
cin>>s1.str;
cout<<"\nEnter String 2 For Comparison: ";
cin>>s2.str;
s1=s2;
break;
case 2: cout<<"\nEnter String: ";
cin>>s1.str;
s2==s1;
break;
case 3: cout<<"\nEnter String 1: ";
cin>>s1.str;
cout<<"\nEnter String 2: ";
cin>>s2.str;
s1+s2;
break;
case 4: cout<<"\nEnter String to Reverse: ";
cin>>s1.str;
cout<<"Reversed String is:- ";
cout<
break;
case 5: cout<<"\nEnter String: ";
cin>>s1.str;
s2<=s1;
break;
case 6: cout<<"\nEnter String: ";
cin>>s1.str;
cout<<"\nEnter Sub-String: ";
cin>>s2.str;
s1>=s2;
break;
case 7: exit(1);
}
cout<<"\n\nDo You want to Continue(Y/N) : ";
ans=getche();
} while(ans=='y'||ans=='Y');
getch();
}
iostream.h
conio.h
process.h
class string
{
public:
char *str;
void operator=(string);
void operator==(string);
void operator+(string);
void operator<=(string);
void operator>=(string);
friend ostream* operator<<(ostream*,string *s1);
friend istream& operator>>(istream&,string &s1);
};
// String Comparison
void string::operator=(string s2)
{
int i,j,flag=0;
for(i=0;s2.str[i]!='\0'||str[i]!='\0';i++)
{
if(s2.str[i]!=str[i])
{
flag=1;
}
}
if(flag==0)
cout<<"\nStrings are equal";
else
cout<<"\nString are not equal";
}
// String Copy
void string::operator==(string s2)
{
int i,j,c=0;
for(i=0;s2.str[i]!='\0';i++)
{
str[i]=s2.str[i];
c++;
}
cout<<"\nCopied string is:- ";
for(i=0;i<=c;i++)
cout<
}
// String Concatenatinon
void string::operator+(string s2)
{
int i,j;
for(i=0;str[i]!='\0';i++);
for(j=0;str[j]!='\0';j++)
{
str[i]=s2.str[j];
i++;
}
cout<<"\nConcated String is:- ";
for(i=0;str[i]!='\0';i++)
cout<
}
istream & operator >> (istream &in,string &s1)
{
for(int i=0;s1.str[i]!='\0';i++)
in>>s1.str[i];
return in;
}
// String Reverse
ostream& operator << (ostream &out,string &s1)
{
for(int i=0;s1.str[i]!='\0';i++);
for(int j=i;j>=0;j--)
out<
return out;
}
// String Palindrome Check
void string::operator<=(string s1)
{
int i,flag=1,j=0;
for(i=0;s1.str[i]!='\0';i++)
j++;
j--;
for(i=0;s1.str[i]!='\0';i++,j--)
if(s1.str[j]!=s1.str[i])
flag=0;
if(flag==1)
cout<<"\nString is Palindrome";
else
cout<<"\nString is not Palindrome";
}
// String SubString
void string::operator>=(string s2)
{
int flag=0,j=0,i;
for(i=0;str[i]!='\0';i++)
{
if(str[i]==s2.str[j])
j++;
else
j=0;
if(s2.str[j]=='\0')
{
cout<<"\nSub-String is Present";
flag=1;
}
}
if(flag==0)
cout<<"\nSubstring is Absent";
}
// Main Function
void main()
{
int ch;
string s1,s2;
char ans;
clrscr();
do
{
cout<<"\n---------Menu---------";
cout<<"\n\n1.Equality";
cout<<"\n2.Copy string";
cout<<"\n3.Concat ";
cout<<"\n4.Reverse";
cout<<"\n5.Palindrom";
cout<<"\n6.Substring ";
cout<<"\n7:Exit";
cout<<"\n\nEnter your Choice : ";
cin>>ch;
switch(ch)
{
case 1: cout<<"\nEnter String 1 For Comparison: ";
cin>>s1.str;
cout<<"\nEnter String 2 For Comparison: ";
cin>>s2.str;
s1=s2;
break;
case 2: cout<<"\nEnter String: ";
cin>>s1.str;
s2==s1;
break;
case 3: cout<<"\nEnter String 1: ";
cin>>s1.str;
cout<<"\nEnter String 2: ";
cin>>s2.str;
s1+s2;
break;
case 4: cout<<"\nEnter String to Reverse: ";
cin>>s1.str;
cout<<"Reversed String is:- ";
cout<
break;
case 5: cout<<"\nEnter String: ";
cin>>s1.str;
s2<=s1;
break;
case 6: cout<<"\nEnter String: ";
cin>>s1.str;
cout<<"\nEnter Sub-String: ";
cin>>s2.str;
s1>=s2;
break;
case 7: exit(1);
}
cout<<"\n\nDo You want to Continue(Y/N) : ";
ans=getche();
} while(ans=='y'||ans=='Y');
getch();
}
Comments
Post a Comment