Oxaric’s Blog

A compendium of amazing things…

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;
}

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>