Oxaric’s Blog

A compendium of amazing things…

Posts Tagged ‘string’

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;

}


Posted in C/C++, Programming | Tagged: , , , , , , , , , , , , | Leave a Comment »

3 Simple C++ String Functions

Posted by oxaric on November 23, 2008

Here are some simple C++ functions I thought I’d put up. Maybe I’ll put up more later.


The 3 functions are:
string trim( string line );
string prepareLineForLinux( string line );
bool isWhiteLine( string line );


trim()
input: receives a string
output: returns a new string with all leading and ending whitespace removed


prepareLineForLinux()
input: receives a string
output: returns a new string that will be acceptable to be used in a Linux system call. Checks for Linux special characters and includes the necessary delimiters.


isWhiteLine()
needs the function trim()
input: receives a string
output: returns true if the string is empty otherwise false


Click to directly download stringfunctions.cpp

// filename 'stringfunctions.cpp'
// Use: Has some functions used for manipulating strings
// By: Louis Casillas, oxaric@gmail.com

#include <iostream>
#include <string>

using namespace std;

string trim( string line );
string prepareLineForLinux( string line );
bool isWhiteLine( string line );

// needs the function trim()
// input: receives a string
// output: returns true if the string is empty otherwise false
bool isWhiteLine( string line )
{
   string temp = trim(line);

   if ( (temp == "") || (temp == "\n") )
   {
      return true;
   }
   else
   {
      return false;
   }
}

// input: receives a string
// output: returns a new string with all leading and ending whitespace removed
string trim( string line )
{
   if ( line.empty() )
   {
      return "";
   }

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

   // the minus 1 is needed to start at the first character
        // and skip the string delimiter
   int end_of_string = string_size - 1;
   
   bool encountered_characters = false;
   
   // find the start of chracters in the string
   while ( (beginning_of_string < string_size) && (!encountered_characters) )
   {
      // if a space or tab was found then ignore it
      if ( (line[ beginning_of_string ] != ' ') && (line[ beginning_of_string ] != '\t') )
      {
         encountered_characters = true;
      }
      else
      {         
         beginning_of_string++;
      }
   }

   // test if no characters were found in the string
   if ( beginning_of_string == string_size )
   {
      return "";
   }
   
   encountered_characters = false;

   // find the character in the string
   while ( (end_of_string > beginning_of_string) && (!encountered_characters) )
   {
      // if a space or tab was found then ignore it
      if ( (line[ end_of_string ] != ' ') && (line[ end_of_string ] != '\t') )
      {
         encountered_characters = true;
      }
      else
      {         
         end_of_string--;
      }
   }   
   
   // return the original string with all whitespace removed from its beginning and end
   // + 1 at the end to add the space for the string delimiter
   return line.substr( beginning_of_string, end_of_string - beginning_of_string + 1 );
}

// input: receives a string
// output: returns a new string that will be acceptable to be used in a Linux system call
//         checks for Linux special characters and includes the necessary delimiters
string prepareLineForLinux( string line )
{
   string return_string = "";
   
   int i = 0;
   int string_size = (int)(line.size() - 1);
   while ( i <= string_size )
   {
      if ( line[i] == ' ' )
      {
         return_string += "\\ ";
      }
      else if ( line[i] == '\'' )
      {
         return_string += "\\\'";
      }
      else if ( line[i] == '\\' )
      {
         return_string += "\\\\";
      }
      else if ( line[i] == '(' )
      {
         return_string += "\\(";
      }      
      else if ( line[i] == ')' )
      {
         return_string += "\\)";
      }
      else if ( line[i] == '[' )
      {
         return_string += "\\[";
      }      
      else if ( line[i] == ']' )
      {
         return_string += "\\]";
      }
      else if ( line[i] == '&' )
      {
         return_string += "\\&";
      }
      else
      {
         return_string += line[i];
      }

      i++;
   }
         
   return return_string;
}

int main()
{
   cout << "\ntrim() function\n---\n";

   string test1 = \t  Hello World!\t \t ";
   cout << "\nOriginal Text: |" << test1 << "|\n";
   cout << "Trimmed Text: |" << trim(test1) << "|\n";

   cout << "\nprepareLineForLinux() function\n---\n";
   string test2 = "mv /this is/ my/ directory[0]/file-(4).txt";

   cout << "\nOriginal Text: |" << test2 << "|\n";
   cout << "Prepared Text: |" << prepareLineForLinux(test2) << "|\n";
   
   cout << "\nisWhiteLine() function\n---\n";

   string test3 = " ";
   cout << "\nOriginal Text: |" << test3 << "|\n";
   cout << "isWhiteLine?: " << isWhiteLine(test3) << " <--- 0=NO, 1=YES \n";

   return 0;
}

Posted in C/C++, Linux, Programming | Tagged: , , , , , , , , , , , | Leave a Comment »