Oxaric’s Blog

A compendium of amazing things…

Posts Tagged ‘c++’

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 »

Euler Project Problem #104 Solution

Posted by oxaric on November 24, 2008

It uses C.

Click to directly download euler-solution-104.c
// filename 'euler-solution-104.c'
// By: Louis Casillas, oxaric@gmail.com

// Euler Problem #104
// Find the Fibonacci sequence for which the first nine digits and the last nine digits are 1-9 pandigital

#include <stdio.h>
#include <gmp.h>

char number_string[100000];

char isPandigital( unsigned long int num )
{
   if ( num < 123456789 )
   {
      return 0;
   }

   char num_of_unique_digits = 0;

   char number_array[9];

   unsigned long int temp = num;

   int i;
   int j;

   for ( i = 0; i < 9; i++ )
   {
      number_array[i] = temp - ((temp / 10) * 10);
      if ( number_array[i] == 0 )
      {
         return 0;
      }

      temp /= 10;
   }   

   char is_unique = 1;
   
   for ( i = 0; i < 9; i++ )
   {
      for ( j = 0; j < 9; j++ )
      {
         if ( i != j )
         {
            if ( number_array[i] == number_array[j] )
            {
               is_unique = 0;
            }
         }
      }
      
      if ( is_unique )
      {
         num_of_unique_digits++;
      }

      is_unique = 1;
   }   

   if ( num_of_unique_digits == 9 )
   {
      return 1;
   }
   else
   {
      return 0;
   }
}

int numOfDigits( mpz_t num )
{
   int num_of_digits = 0;
   
   char *number_pointer;
   mpz_get_str( number_string, 10, num );
   
   number_pointer = number_string;

   while( *number_pointer != '' )
   {
      num_of_digits++;
      number_pointer++;
   }

   return num_of_digits;
}

char areBothSidesPandigital( mpz_t num )
{
   mpz_t mod;
   mpz_init_set_str( mod, "1000000000", 10 );

   mpz_t temp;
   mpz_init( temp );

   unsigned long int number_group = 0;

   mpz_mod( temp, num, mod );

   number_group = mpz_get_ui( temp );

   if ( isPandigital( number_group ) )
   {
      mpz_ui_pow_ui( temp, 10, (numOfDigits( num ) - 9) );

      mpz_tdiv_q( temp, num, temp );

      number_group = mpz_get_ui( temp );

      if ( isPandigital( number_group ) )
      {
         mpz_clear( temp );
         mpz_clear( mod );

         return 1;
      }
   }
   
   mpz_clear( temp );
   mpz_clear( mod );

   return 0;
}

int main()
{
   mpz_t num1;
   mpz_init_set_str( num1, "1", 10 );

   mpz_t num2;
   mpz_init_set_str( num2, "1", 10 );

   char add_to_first = 1;
   char are_we_up_to_ten_digits = 0;
   int current_sequence = 2;

   while( 1 )
   {
      current_sequence++;

      if ( add_to_first )
      {
         mpz_add( num1, num1, num2 );
         add_to_first = 0;
      }
      else
      {
         mpz_add( num2, num1, num2 );
         add_to_first = 1;
      }

      if ( add_to_first )
      {
   //      printf( "\nNumber of digits: %d\n", numOfDigits( num2 ) );
         if ( areBothSidesPandigital( num2 ) )
         {
            printf( "\n\nThe double pandigital Fibonacci sequence was found at: %d\n\n", current_sequence );
            return 0;
         }
      }
      else
      {
         //printf( "\nNumber of digits: %d\n", numOfDigits( num1 ) );
         if ( areBothSidesPandigital( num1 ) )
         {
            printf( "\n\nThe double pandigital Fibonacci sequence was found at: %d\n\n", current_sequence );
            return 0;
         }
      }      
   }

   //mpz_ui_pow_ui( temp, base, exp );

   //mpz_out_str( out_file, 10, temp );

   //printf("\n");
   //mpz_out_str( stdout, 10, temp );
   //printf("\n\n");

   printf("\n\n");

   return 0;
}

Posted in C/C++, Programming, Project Euler | Tagged: , , , , , , | 2 Comments »

Euler Project Problem #102 Solution

Posted by oxaric on November 24, 2008

It uses C.


Click to directly download euler-solution-102.c

Right click to save the needed data file triangles.txt

// filename 'euler-solution-102.c'
// By: Louis Casillas, oxaric@gmail.com

// Euler Problem #102
// Determine which triangles encompass the origin.
// The file is 'triangles.txt'

#include <stdio.h>

char doesLineCrossXAxis( x1, x2 )
{
   if ( ((x1 < 0) && (x2 > 0)) || ((x1 > 0) && (x2 < 0)) )
   {
      return 1;
   }
   
   return 0;
}

char doesLineCrossYAxis( y1, y2 )
{
   if ( ((y1 < 0) && (y2 > 0)) || ((y1 > 0) && (y2 < 0)) )
   {
      return 1;
   }
   
   return 0;
}

char doesTriangleContainOrigin( x1, y1, x2, y2, x3, y3 )
{
   char does_line1_cross_x_axis = 0;
   char does_line2_cross_x_axis = 0;
   char does_line3_cross_x_axis = 0;

   char num_lines_cross_x_axis = 0;

   // line1
   if ( doesLineCrossXAxis( x1, x2 ) )
   {
      does_line1_cross_x_axis = 1;
      num_lines_cross_x_axis++;
   }

   // line2
   if ( doesLineCrossXAxis( x2, x3 ) )
   {
      does_line2_cross_x_axis = 1;
      num_lines_cross_x_axis++;
   }

   // line3
   if ( doesLineCrossXAxis( x1, x3 ) )
   {
      does_line3_cross_x_axis = 1;
      num_lines_cross_x_axis++;
   }

   // two of the lines must cross the x-axis to make it possible to contain the origin
   if ( num_lines_cross_x_axis < 2 )
   {
      return 0;
   }

   float line1_slope = 0;
   float line2_slope = 0;
   float line3_slope = 0;

   float line1_x_intercept = 0;
   float line2_x_intercept = 0;
   float line3_x_intercept = 0;

   if ( does_line1_cross_x_axis )
   {
      line1_slope = (1.0 * y1 - y2) / (x1 - x2);
      line1_x_intercept = y1 - ( 1.0 * line1_slope * x1 );      
   }

   if ( does_line2_cross_x_axis )
   {
      line2_slope = (1.0 * y2 - y3) / (x2 - x3);
      line2_x_intercept = y2 - ( 1.0 * line2_slope * x2 );
   }

   if ( does_line3_cross_x_axis )
   {
      line3_slope = (1.0 * y1 - y3) / (x1 - x3);
      line3_x_intercept = y3 - ( 1.0 * line3_slope * x3 );
   }

   if ( ((line1_x_intercept > 0) && (line2_x_intercept < 0)) ||
             ((line2_x_intercept > 0) && (line3_x_intercept < 0)) ||
             ((line1_x_intercept > 0) && (line3_x_intercept < 0)) ||
        ((line1_x_intercept < 0) && (line2_x_intercept > 0)) ||
             ((line2_x_intercept < 0) && (line3_x_intercept > 0)) ||
             ((line1_x_intercept < 0) && (line3_x_intercept > 0))

           )
   {
      return 1;
   }

   return 0;
}

int main()
{
   int number_holder = 0;
   char current_point = 1;
   char current_char;
   char is_negative_number = 0;
   int num_that_contain_origin = 0;

   int x1 = 0;
   int y1 = 0;
   int x2 = 0;
   int y2 = 0;
   int x3 = 0;
   int y3 = 0;

//printf( "\n\n%d\n\n", (int)doesTriangleContainOrigin( -340, 495, -153, -910, 835, -947 ) );

   FILE *in_file = fopen("triangles.txt", "rt");

   while( (current_char = fgetc( in_file )) != EOF )
   {   
      if ( current_char == '\r' )
      {
      }
      else if ( current_char == '\n' )
      {
         if ( is_negative_number )
         {
            number_holder *= -1;
         }
         y3 = number_holder;
         number_holder = 0;
         is_negative_number = 0;
         
//         printf( "\n\n%d\t%d\t%d\t%d\t%d\t%d", x1, y1, x2, y2, x3, y3 );
//getchar();

         if ( doesTriangleContainOrigin( x1, y1, x2, y2, x3, y3 ) )
         {
            num_that_contain_origin++;
         }

         current_point = 1;

         x1 = 0;
         y1 = 0;
         x2 = 0;
         y2 = 0;
         x3 = 0;
         y3 = 0;
      }
      else if ( current_char == ',' )
      {
         if ( is_negative_number )
         {
            number_holder *= -1;
         }

         switch( current_point )
         {
            case 1:
               x1 = number_holder;
            break;

            case 2:
               y1 = number_holder;
            break;

            case 3:
               x2 = number_holder;
            break;

            case 4:
               y2 = number_holder;
            break;

            case 5:
               x3 = number_holder;
            break;
         }

         current_point++;

         number_holder = 0;
         is_negative_number = 0;
      }
      else
      {
         // ascii value of '-' is 45, base 10
         
         if ( (int)current_char == 45 )
         {
            is_negative_number = 1;
         }
         else
         {
            number_holder *= 10;
            number_holder += (current_char - '0');
         }
      }
   }

   printf( "\n\nThe number of triangles that contain the origin is: %lu\n\n", num_that_contain_origin );
   
   fclose( in_file );

   printf("\n\n");

   return 0;
}

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

Euler Project Problem #99 Solution

Posted by oxaric on November 24, 2008

It uses C.


Click to directly download euler-solution-99.c

Right click to save the needed data file base_exp.txt

// filename 'euler-solution-99.c'
// By: Louis Casillas, oxaric@gmail.com

// Euler Problem #99
// Determine which line number that has the greatest numerical value.
// Calculate by: first number ^ second number
// The file is 'base_exp.txt'

#include <stdio.h>
#include <gmp.h>

int main()
{
   mpz_t temp_result;
   mpz_init( temp_result );

   mpz_t max_result;
   mpz_init( max_result );
   mpz_set_ui( max_result, 0 );

   unsigned long int base;
   unsigned long int exp;
   unsigned long int number_holder = 0;
   
   unsigned int line_number = 0;
   unsigned int max_line_number = 1;

   char current_char;

   FILE *in_file = fopen("base_exp.txt", "rt");

   while( (current_char = fgetc( in_file )) != EOF )
   {   
      if ( current_char == '\r' )
      {
      }
      else if ( current_char == '\n' )
      {
         exp = number_holder;
         number_holder = 0;
         line_number++;

         mpz_ui_pow_ui( temp_result, base, exp );
         
         if ( mpz_cmp( temp_result, max_result ) > 0 )
         {
            mpz_set( max_result, temp_result );
            max_line_number = line_number;
         }
         
         printf( "\nline number: %d\n", line_number );

         base = 0;
         exp = 0;
      }
      else if ( current_char == ',' )
      {
         base = number_holder;
         number_holder = 0;
      }
      else
      {
         number_holder *= 10;
         number_holder += (current_char - '0');
      }
   }

   printf( "\n\nThe max line number is: %lu\n\n", max_line_number );

   //mpz_ui_pow_ui( temp, base, exp );

   //mpz_out_str( out_file, 10, temp );

   //printf("\n");
   //mpz_out_str( stdout, 10, temp );
   //printf("\n\n");
   
   fclose( in_file );

   printf("\n\n");

   return 0;
}

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

Euler Project Problem #34 Solution

Posted by oxaric on November 24, 2008

It uses C++.


Click to directly download euler-solution-34.cpp

// filename 'euler-solution-34.cpp'
// By: Louis Casillas, oxaric@gmail.com

// Euler Problem #34
// Find the sum of all numbers which are equal to the sum of the factorial of their digits.
// Example: 145 = 1! + 4! + 5! = 145

#include <iostream>

using namespace std;

int fact( int n )
{
   if ( n == 0 )
   {
      return 1;
   }

   if ( n == 1 )
   {
      return 1;
   }

   if ( n == 2 )
   {
      return 2;
   }

   int total = 1;
   
   int i;
   for ( i = 2; i <= n; i++ )
   {
      total *= i;
   }

   return total;
}

int followsTheRules( int num )
{
   int total = 0;
   int temp = num;

   while( temp >= 1 )
   {
      total += fact( temp % 10 );
      temp /= 10;
   }

   //cout << "\ntotal = " << total << "\n\n";

   if ( num == total )
   {
      return 1;
   }
   else
   {
      return 0;
   }
}

int main()
{
   cout.setf( ios_base::fixed, ios_base::floatfield );
   cout.precision( 0 );
   
   int total = 0;
   int counter = 3;
   
   while( counter < 50000 )
   {
      if ( followsTheRules( counter ) )
      {
         total += counter;
      }
   
      counter++;
   }

   cout << "\n\nSolution: " << total << "\n\n"; 
   
   return 0;
}

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

Euler Project Problem #28 Solution

Posted by oxaric on November 24, 2008

It uses c++.


Click to directly download euler-solution-28.cpp

// filename 'euler-solution-28.cpp'
// By: Louis Casillas, oxaric@gmail.com

// Euler Problem #28
// What is the sum of both diagonals in a 1001 by 1001 number spiral.
// Example: This is a 5x5 spiral
// 21 22 23 24 25
// 20 07 08 09 10
// 19 06 01 02 11
// 18 05 04 03 12
// 17 16 15 14 13

#include <iostream>

using namespace std;

#define MAX_SPIRAL_NUMBER 1002001 // 1001 * 1001

int main()
{
   cout.setf( ios_base::fixed, ios_base::floatfield );
   cout.precision( 0 );
   
   int adder = 2;
   int counter = 0;
   int current_number = 1;
   double total = 0;
   
   while ( current_number <= MAX_SPIRAL_NUMBER )
   {
      total += current_number;
      current_number += adder;      
      //cout << "\n" << total << "\n";getchar();

      counter++;

      if ( counter == 4 )
      {
         counter = 0;
         adder += 2;
      }
   }
   total++;

   cout << "\n\nSolution: " << total << "\n\n"; 
   
   return 0;
}

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