Consider the following Class mystring { char str [100]; Public: // methods }; Overload operator “!” to reverse the case of each alphabet in the string.

#include<iostream.h>
#include<string.h>
#include<ctype.h>
#include<conio.h>

class mystring
{
  char str[100];

  public:
  void getstring()
  {
cout<<endl<<"Enter string: ";
cin>>str;
  }

  void operator!()
  {
int l=strlen(str);
for(int i=0;i<l;i++)
{
 if(isalpha(str[i]))
 {
if(islower(str[i]))
 str[i]=toupper(str[i]);
else
 str[i]=tolower(str[i]);
 }
}
  }

  void display()
  {
cout<<str;
  }
};//End of class

int main()
{
  mystring ms;
  clrscr();
  ms.getstring();
  cout<<endl<<"String is: ";
  ms.display();
  !ms;
  cout<<endl<<"String after changing cases: ";
  ms.display();
  getch();
  return 0;
}

No comments:

Post a Comment