Thursday, 7 March 2019

CPP-EX12

WAP to use operator  !,[] using class

#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<stdio.h>
#include<process.h>
class ms
{
 char *s;
public:
ms();
void dis();
ms operator!();
void operator[](ms);
};
ms::ms()
{
 int n;
cout<<"\nEnter the len string";
 cin>>n;
s=new char[n+1];
cout<<"\nEnter string";
for(int i=0;i<n;i++)
cin>>(s+i);
*(s+i)='\0';
}
void ms::dis()
{
cout<<s;
}
ms ms::operator!()
{ for(int i=0;*(s+i)!='\0';i++)
 if((*(s+i)>=65)&&(*(s+i)<=91))
   (*(s+i))=(*(s+i))+32;
else if((*(s+i)>=97)&&(*(s+i)<=122))
   (*(s+i))=(*(s+i))-32;
cout<<s;
   return *this;

}

void ms::operator[](ms str)
{ int n;
cout<<"\neNter the index: ";
cin>>n;
for(int i=0;i!=n;i++)
cout<<"character at "<<n<<"is"<<(*(str.s+i));
}

void main()
{
 ms m1;
 clrscr();
 int ch;
cout<<"\nstring  is: ";
m1.dis();
while(1)
{cout<<"\n1.change case";
cout<<"\n2.find character";
cout<<"\n4.exit";
cin>>ch;
switch(ch)
{
            case 1:
  cout<<"\nchange case";
!m1;
break;
case 2:
cout<<"\nfind character";
m1[m1];
m1.dis();
break;
case 4:exit(0);
break;
}
}

getch();
}