C++ program to perform String operations
Write a C++ program to perform String operations i. = Equality ii. == String Copy iii. + Concatenation iv. << To display a string v. >> To reverse a string vi. Function to determine whether a string is a palindrome To find occurrence of a sub-string. Use Operator Overloading
#include<iostream.h>
#include<conio.h>
class string
{
private:
char str[10];
public:
char str1[20];
void accept();
void operator<<(string s2);//display
void operator>>(string & str);//reverse
void operator+(string &s1); //concate
void operator=(string s2);//equality
void operator==(string & s3);//copy
};
void string::accept()
{
cout<<"\nEnter string :=> ";
cin>>str1;
}
void string::operator<<(string s2)
{
cout<<"\nString1=>"<<str1;
cout<<"\nString2=>"<<s2.str1;
}
void string::operator>>(string & str)
{
int i=0,j=0;
char t;
while(str.str1[i]!='\0')
i++;
i--;
while(i>j)
{
t=str.str1[i];
str.str1[i]=str.str1[j];
str.str1[j]=t;
i--;
j++;
}
}
void string::operator+(string &s1)
{
int i=0,j=0;
while(s1.str1[i]!='\0')
i++;
while(str1[j]!='\0')
{
s1.str1[i]=str1[j];
i++;
j++;
}
s1.str1[i]='\0';
}
void string::operator=(string s2)
{
int i=0;
while(str1[i]!='\0'&&s2.str1[i]!='\0')
{
if(str1[i]!=s2.str1[i])
{
cout<<"\nNot equal";
break;
}
i++;
}
if(str1[i]=='\0'&&s2.str1[i]=='\0')
cout<<"Strings are equal";
}
void string::operator==(string & s3)
{
int i=0;
while(str1[i]!='\0')
{
s3.str1[i]=str1[i];
i++;
}
s3.str1[i]='\0';
}
void main()
{
clrscr();
int ch;
string s1,s2,s3;
do
{
cout<<"\n\n1.Accept strings\n2.Display\n3.Reverse string\n4.Concatinate";
cout<<"\n5.Check equality\n6.Copy\n7.Exit";
cout<<"\nEnter choice no= ";cin>>ch;
switch(ch)
{
case 1:
s1.accept();
s2.accept();
break;
case 2:
cout<<"\nDisplay.....";
s1<<s2;
break;
case 3:
cout<<"\nReverse IInd str ......";
s1>>s2;
s1<<s2;
break;
case 4:
cout<<"\nConcatinate......";
s2+s1;
cout<<"\nString="<<s1.str1;
break;
case 5:
s1=s2;
break;
case 6:
s1==s3;
cout<<"\nCopied string = >"<<s3.str1;
break;
}
}while(ch!=7);
getch();
}
OUTPUT :
1.Accept strings
2.Display
3.Reverse string
4.Concatinate
5.Check equality
6.Copy
7.Exit
Enter choice no= 1
Enter string :=> abc
Enter string :=> xyz
1.Accept strings
2.Display
3.Reverse string
4.Concatinate
5.Check equality
6.Copy
7.Exit
Enter choice no= 2
Display.....
String1=>abc
String2=>xyz
1.Accept strings
2.Display
3.Reverse string
4.Concatinate
5.Check equality
6.Copy
7.Exit
Enter choice no= 3
Reverse IInd str ......
String1=>abc
String2=>zyx
1.Accept strings
2.Display
3.Reverse string
4.Concatinate
5.Check equality
6.Copy
7.Exit
Enter choice no= 4
Concatinate......
String=abczyx
1.Accept strings
2.Display
3.Reverse string
4.Concatinate
5.Check equality
6.Copy
7.Exit
Enter choice no= 5
Not equal
1.Accept strings
2.Display
3.Reverse string
4.Concatinate
5.Check equality
6.Copy
7.Exit
Enter choice no= 6
Copied string = >abczyx
1.Accept strings
2.Display
3.Reverse string
4.Concatinate
5.Check equality
6.Copy
7.Exit
Enter choice no= 7
1 comments:
what is the difference between concatenation of string and copying string because both the output look similar....
Post a Comment