Create a class MyString which contains a character pointer (Use new and delete operator). Write a C++ program to overload following operators: ! To change the case of each alphabet from given string [ ] To print a character present at specified index
//Note: If you are not using Borland or Turbo C then add the following line after header files :
//using namespace std;
#include<iostream.h>
#include<conio.h>
class mystring
{
public:
int n;
int i,c;
char *p; //p=string
void accept()
{
cout<<"Enter how many lenght of string: ";
cin>>n;
p=new char[n+1]; // allocate memory dynamically
cout<<"Enter string: ";
cin>>p;
}
void disp()
{
cout<<"\nstring is: "<<p;
}
void operator !()
{
c=strlen(p); // strlen() is function is used to lenght of string
for(i=0;i<n;i++)
{
if(p[i]>64&&p[i]<91)
{
p[i]=p[i]+32;
}
else
{
p[i]=p[i]-32;
}
}
cout<<"\nChange string is: "<<p;
}
void operator [](int l)
{
int e;
char m;
e=l;
for(i=0;i<c;i++)
{
if(e-1==i)
{
cout<<"Character is: "<<p[i];
}
}
}
};
int main()
{
int e,ch; // e=index
do{
cout<<"\n\n1.Accept string\n2.Display string\n3.Change the case of each alphabet\n4.print a character present at specified index\n5.Exit\nEnter your choice :- ";
cin>>ch;
switch(ch)
{
case 1: mystring m;
m.accept();break;
case 2: m.disp();break;
case 3: !m;break;
case 4: cout<<"\nEnter index: ";
cin>>e;
m[e];break;
}
}while(ch!=5);
}
0 Comments