Oxaric’s Blog

A compendium of amazing things…

Letter Case C++ String Functions

Posted by oxaric on November 25, 2008

I spent some time writing some C++ functions to mess around with a string’s letter cases. For more detailed info about each function take a look at the comments in the code.


The functions are:


int nocasecomp( string line1, string line2 )


string toLowerCase( string line )
string toUpperCase( string line )
string capitalize( string line )
string incrementCase( string line, bool first_character_to_upper )
string randomizeCase( string line )
string swapCase( string line )



These functions do the same thing as the ones above but they change the passed string in-place:
    void toLowerCase_in( string &line )
    void toUpperCase_in( string &line )
    void capitalize_in( string &line )
    void incrementCase_in( string &line, bool first_character_to_upper )
    void randomizeCase_in( string &line )
    void swapCase_in( string &line )



If you compile and run the program it demonstrates the functions:

Original String: Hello World!


nocasecomp( "Hello World!", "HeLLo wOrLd!" ) => 0


toLowerCase( "Hello World!" ) => hello world!


toUpperCase( "Hello World!" ) => HELLO WORLD!


capitalize( "Hello World!" ) => Hello world!


swapCase( "Hello World!" ) => hELLO wORLD!


incrementCase( "Hello World!", true ) => HeLlO wOrLd!


randomizeCase( "Hello World!" ) => heLlO WorLD!


Click to directly download CaseStringFunctions.cpp

// filename 'CaseStringFunctions.cpp'

// Use: Has some functions used for manipulating string cases

// By: Louis Casillas, oxaric@gmail.com



#include <iostream>

#include <string>


using namespace std;


bool isALetter( char ch );
bool isLowerCaseLetter( char ch );

bool isUpperCaseLetter( char ch );

int nocasecomp( string line1, string line2 );



// These functions all return a new string

string toLowerCase( string line );


string toUpperCase( string line );

string capitalize( string line );

string incrementCase( string line, bool first_character_to_upper );


string randomizeCase( string line );

string swapCase( string line );


// These functions all change the passed string in-place


void toLowerCase_in( string &line );

void toUpperCase_in( string &line );

void capitalize_in( string &line );


void incrementCase_in( string &line, bool first_character_to_upper );

void randomizeCase_in( string &line );


void swapCase_in( string &line );


// returns true if the character is a letter


bool isALetter( char ch )

{

   if ( (ch >= 65 && ch <= 90) || (ch >= 97 && ch <= 122) )


   {

      return true;

   }

   else

   {

      return false;

   }


}


// returns true if the character is an upper case letter

bool isUpperCaseLetter( char ch )


{

   if ( (ch >= 65) && (ch <= 90) )

   {


      return true;

   }

   else

   {

      return false;

   }

}



// returns true if the character is a lower case letter

bool isLowerCaseLetter( char ch )


{

   if ( (ch >= 97) && (ch <= 122) )

   {


      return true;

   }

   else

   {

      return false;

   }

}



// compares two strings for equality while ignoring letter case

// if the strings are not equal then it returns the 


// alphabetical ordering of the strings


// returns 0 if the strings are equal


// returns -1 if line1 is alphabetically lower than line2

// returns 1 if line1 is alphabetically higher than line2



// if line1 is longer than line2 but they are both

// equal up until line2 ends then this will return 1 as


// line1 is the longer string, the reverse will return -1 

int nocasecomp( string line1, string line2 )


{

   string temp1 = toLowerCase( line1 );

   string temp2 = toLowerCase( line2 );



   return temp1.compare( temp2 );

}


// takes a string and converts the letters to lower-case


// returns a new string

string toLowerCase( string line )

{

   string new_line = line;



   toLowerCase_in( new_line );


   return new_line;

}


// takes a string and converts the letters to lower-case


// does so in-place.

void toLowerCase_in( string &line )

{

   int string_size = (int)(line.length());



   if ( string_size == 0 )

   {

      return;

   }


   for ( int i = 0; i < string_size; i++ )


   {

      if ( isALetter( line[i] ) && isUpperCaseLetter( line[i] ) )


      {

         line[i] = line[i] + 32;

      }


   }

}


// takes a string and converts the letters to upper-case

// returns a new string


string toUpperCase( string line )

{

   string new_line = line;


   toUpperCase_in( new_line );



   return new_line;

}


// takes a string and converts the letters to upper-case


// does so in-place

void toUpperCase_in( string &line )

{

   int string_size = (int)(line.length());



   if ( string_size == 0 )

   {

      return;

   }


   for ( int i = 0; i < string_size; i++ )


   {

      if ( isALetter( line[i] ) && (line[i] >= 97) && (line[i] <= 122) )


      {

         line[i] = line[i] - 32;

      }


   }

}


// takes a string and converts the letters to lower-case

// if the first character is a letter it will make it upper-case


// returns a new string

string capitalize( string line )

{

   string new_line = line;



   capitalize_in( new_line );


   return new_line;

}


// takes a string and converts the letters to lower-case


// if the first character is a letter it will make it upper-case

// does so in-place


void capitalize_in( string &line )

{

   if ( line == "" )

   {


      return;

   }


   toLowerCase_in( line );


   if ( isALetter( line[0] ) )


   {

      line[0] = line[0] - 32;

   }


}


// takes a string and reverses all letter cases

// returns a new string


string swapCase( string line )


{

   string new_line = line;

   

   swapCase_in( new_line );



   return new_line;

}


// takes a string and reverses all letter cases


// does so in-place

void swapCase_in( string &line )

{

   int string_size = (int)(line.length());



   if ( string_size == 0 )

   {

      return;

   }


   char ch;



   for ( int i = 0; i < string_size; i++ )

   {


      ch = line[i];


      if ( isALetter( ch ) ) 

      {


         if ( isLowerCaseLetter( ch ) )

         {

            line[i] = ch - 32;


         }

         else

         {

            line[i] = ch + 32;

         }


      }

   }

}


// takes a string and makes each letter be the opposite case


// to the letter before and after it.

// if true is passed then the first letter in the string will be


// upper case, otherwise the first letter will be lower case

// returns a new string


string incrementCase( string line, bool first_character_to_upper )

{

   string new_line = line;



   incrementCase_in( new_line, first_character_to_upper );


   return new_line;

}


// takes a string and makes each letter be the opposite case


// to the letter before and after it.

// if true is passed then the first letter in the string will be


// upper case, otherwise the first letter will be lower case

// does so in-place


void incrementCase_in( string &line, bool first_character_to_upper )

{

   int string_size = (int)(line.length());



   if ( string_size == 0 )

   {

      return;

   }


   char ch;



   bool to_upper_case = first_character_to_upper;


   for ( int i = 0; i < string_size; i++ )


   {

      ch = line[i];


      if ( isALetter( ch ) ) 


      {

         if ( isLowerCaseLetter( ch ) )

         {

            if ( to_upper_case )


            {

               line[i] = ch - 32;

            }

         }

         else


         {

            if ( !to_upper_case )

            {

               line[i] = ch + 32;


            }

         }


         to_upper_case = !to_upper_case;

      }

   }

}



// takes a string and randomly change the case of the letters

// returns a new string


string randomizeCase( string line )

{

   string new_line = line;


   randomizeCase_in( new_line );



   return new_line;


}


// takes a string and randomly change the case of the letters


// does so in-place

void randomizeCase_in( string &line )

{

   int string_size = (int)(line.length());



   if ( string_size == 0 )

   {

      return;

   }


   srand( time(NULL) );


   

   char ch;


   char should_change_case = 0;


   for ( int i = 0; i < string_size; i++ )


   {

      ch = line[i];


      if ( isALetter( ch ) && (rand() % 2) ) 


      {

         if ( isLowerCaseLetter( ch ) )

         {

            line[i] = ch - 32;


         }

         else

         {

            line[i] = ch + 32;

         }


      }

   }

}


int main()

{

   string test = "Hello World!";   


   cout << "\nOriginal String: " << test << "\n\n";


   cout << "toLowerCase( \"Hello World!\" ) => " << toLowerCase( test ) << "\n\n";


   cout << "toUpperCase( \"Hello World!\" ) => " << toUpperCase( test ) << "\n\n";


   cout << "capitalize( \"Hello World!\" ) => " << capitalize( test ) << "\n\n";


   cout << "swapCase( \"Hello World!\" ) => " << swapCase( test ) << "\n\n";


   cout << "incrementCase( \"Hello World!\", true ) => " << incrementCase( test, true ) << "\n\n";


   cout << "randomizeCase( \"Hello World!\" ) => " << randomizeCase( test ) << "\n\n";



   return 0;

}


Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <pre> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>