c++

c++

c++

Sunday, 14 September 2014

Converting characters using OOP



/*
Write a class "stringz". Pass a string as argument to the constructor of the class and
initialize it to a data member "name". The class should have following member functions :
member function "slength()" to find and return the length of the string
member function "slow()" to convrt the string to locase letters
member function "sup()" to convrt the string to locase letters
*/


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

class  stringz
{
private :
  char  name[15] ;
public :
 stringz  (char  s[15] )  ; //OR  (char  [])
 int  slength()  ;
 void slow() ;
 void sup(void)  ;
} ;

int  main()
{

clrscr()  ;
stringz  st("Pakistan") ;
cout<<"Length : " << st.slength() << endl ;
cout << "Upper case : " ;
st.sup()  ;
cout<<"Lower case : " ;
st.slow()  ;
getch()  ;
return 1 ;
}

stringz::stringz(char s[15])
{
for(int i=0; s[i]!=NULL; i ++)
    name[i] = s[i] ;

// at the end put NULL
name[i] = NULL  ;   //OR  name[i] = '\0' ;
}

int  stringz::slength()
{

 for(int length=0; name[length]!='\0'; length ++) ;
 return  length ;
}

void stringz::slow()
{
for(int i=0; name[i]!='\0'; i ++)
if(name[i] >= 65 && name[i] <= 90)     // A=65 , Z=65+25=90
   name[i] += 32 ;

cout << name << endl ;
}


void stringz::sup()
{
for(int i=0; name[i]!=NULL; i ++)
if(name[i] >= 97 && name[i] <= 122)  // a=97 , Z=97+25=122
   name[i] -= 32 ;

cout << name << endl ;
}

No comments:

Post a Comment