Sample Solution to Lab #3

Here are the 4 functions I showed on Monday

//////////////////////////////////////////////////////////////////////////
//  Student Name :  Dennis Eaton-Hogg
//  Student ID   :  123-450-6789
//  Course       :  CS 162
//  Assignment   :  Lab #3
//  Program File :  string.cpp
//  Due Date     :  7/29/98
//
//  Description  :  This implementation file contains member functions
//		    necessary to create, initialize, copy, and
// 		    manipulate objects of a class named "String."
//		    The private data member of the class, named
//		    "string", will be implemented as a pointer to
//		    a character array on the free store.  Various
//		    manipulations will be performed on the objects, as
//		    detailed in specification file mystring.h.
//		    Manipulations include:
//              	o  copying one object to another (including
//			   pointed-to data)
//   			o  reversing the order of an object's string
//			o  comparing two objects' strings
//  			o  changing an object's string
//			o  displaying an object's string
//			o  counting the total number of characters in
//			   an object's string
//			o  counting the number of occurrences of
//			   a specified character in an object's
//			   string
//
//
/////////////////////////////////////////////////////////////////////


#include "mystring.h"  //  specification file
#include <iostream.h>  //  for I/O
#include <string.h>    //  for string manipulation
#include <assert.h>    //  for debugging
#include <stddef.h>    //  for NULL


/////////////////////////////////////////////////////////////////////
// Constructor (default)
//
// Creates a String and sets it to NULL.
//
// Inputs: None.
//
// Outputs: None.
//
String::String(void)
{
  string = NULL;  
}


/////////////////////////////////////////////////////////////////////
// Constructor
//
// Creates a String and assigns it the value of str.
//
// Inputs: A character string str.
//
// Outputs: None.
//
String::String(char* str)
{
      // if client passes in a NULL pointer, assign it to string
   if (str == NULL)
      string = NULL;
   else
   {
         // create a character array on the free store which is large
         // enough to hold the input string plus the null character,
         // and copy the input string into the array
      string = new char[strlen(str)+1];
      assert (string != NULL);
      strcpy (string,str);
   }
}



/////////////////////////////////////////////////////////////////////
// Destructor
//
// Deletes (from the free store) the array pointed to by the data
// member string.
//
// Inputs: None.
//
// Outputs: None.
//
String::~String(void)
{
   delete[]string;
}


/////////////////////////////////////////////////////////////////////
// Copy constructor
//
// A constructor which is implicitly invoked whenever initialization
// is performed in a declaration, when passing a copy of an actual
// parameter to a formal parameter, and when returning an object
// as the value of a function; allows pointed-to data to be copied.
//
// Inputs: None.
//
// Outputs: None.
//
String::String(const String& otherString)
{
      // if original string is NULL pointer, assign NULL pointer
      // to the new string
   if (otherString.string == NULL)
      string = NULL;
   else
   {
        // create a new array on the free store and copy the existing
        // object's data member into it
      string = new char[strlen(otherString.string)+1];
      assert (string != NULL);
      strcpy (string, otherString.string);
   }
}


/////////////////////////////////////////////////////////////////////
// Function: Reverse
//
// Reverses the object's string data member; if string is a NULL
// pointer, nothing is done to it.
//
// Inputs: None.
//
// Outputs: None.
//
void String::Reverse(void)
{
      // if string is NULL pointer, do nothing
   if (string == NULL)
      ;
   
   else
   {  
   
         // initialize length as length of string
      int length = strlen(string);

         // set the counter to last character in array
      int countLength = length - 1;

         // create a new char array on the free store, pointed
         // to by tempString
      char* tempString = new char[length+1];
      assert (tempString != NULL);

         // loop forward through the tempString array, copy the 
         // original string elements starting from the end of the 
         // array and moving backward, into the tempString array
      for (int i = 0; i < length; i++)
         {
	    tempString[i] = string[countLength];
	    countLength--;
         }
         
         // add the null character to the end of the array
      tempString[length] = '\0';
         
         // delete the original pointed-to array
      delete [] string;

         // reassign the string pointer to the new array
      string = tempString;
   }
}


/////////////////////////////////////////////////////////////////////
// Function: ReverseNew
//
// Returns a new String that is the reverse of itself.  If original
// string is a NULL pointer, the new string is a NULL pointer.
// Creates a new String object and then calls the Reverse() method
// on it.
//
// Inputs: None.
//
// Outputs: A String object with a data member, string, that is the
//          reverse of the calling object's string.
//
String String::ReverseNew(void) const
{
    // create a local object of type String and initialize
    // to private data member of the class
    String strLocal(string);

    // reverse the strLocal String
    strLocal.Reverse();

    // return the new String object
    return strLocal;
}


/////////////////////////////////////////////////////////////////////
// Function: isEqual
//
// Compares two String objects and tests for equality (by character
// set.  Assumes if both string members are NULL pointers, they are
// equal.
//
// Inputs: A String object.
//
// Outputs: TRUE if the objects' data members are the same, FALSE
// if not.
//
Boolean String::isEqual(String otherString) const
{
      // compare string data members, first if either or both
      // are NULL pointers
   if (string == NULL && otherString.string == NULL)
      return TRUE;
   else if (string == NULL && otherString.string != NULL)
      return FALSE;
   else if (string != NULL && otherString.string == NULL)
      return FALSE;
   else
   {
	 // if both are not NULL pointers, compare using strcmp
	 // and return TRUE if they're equal
      if (strcmp(string, otherString.string) == 0)
	 return TRUE;
      else
	 return FALSE;
   }

}



/////////////////////////////////////////////////////////////////////
// Function: isGreater
//
// Compares two String objects' data members.  Assumes string
// non-NULL pointers are greater than string NULL pointers.
//
// Inputs: A String object.
//
// Outputs: TRUE if the calling object is greater than (by character
// set) the input object, FALSE if it is less or equal.
//
Boolean String::isGreater(String otherString) const
{
      // compare string data members, first if either or both
      // are NULL pointers
   if (string == NULL && otherString.string == NULL)
      return FALSE;
   else if (string == NULL && otherString.string != NULL)
      return FALSE;
   else if (string != NULL && otherString.string == NULL)
      return TRUE;
   else
   {
	 // if both are not NULL pointers, compare using strcmp
	 // and return TRUE if the caller's string is greater
	 // than the passed-in object's string
      if (strcmp(string, otherString.string) > 0)
	 return TRUE;
      else
	 return FALSE;
   }

}



/////////////////////////////////////////////////////////////////////
// Function: Set
//
// Replaces an objects member data string with the input character
// string str.
//
// Inputs: A character string str.
//
// Outputs: None.
//
void String::Set(char* str)
{
      // if input string, str, is NULL pointer, 
      // set string to NULL pointer
   if (str == NULL)
      string = NULL;
      
      // if input string is not NULL pointer, delete existing 
      // string, create new array on the free store, and copy 
      // str into it
   else
   {
	 delete[] string;
	 string = new char [ strlen(str)+1 ];
	 assert (string != NULL);
	 strcpy(string, str);
   }      
}


/////////////////////////////////////////////////////////////////////
// Function: Set
// (Deep-copy operation)
// Performs copying of one object to another; allows for copying of
// pointed-to data.
//
// Inputs: The object to be copied.
//
// Outputs: None.
//
void String::Set(const String& otherString)
{
      // if input string is NULL pointer, set string to NULL pointer
   if (otherString.string == NULL)
      string = NULL;

     // if input string is not NULL pointer, delete existing 
     // string, create new array on the free store, and copy 
     // str into it
   else
   {
      delete [] string;
      string = new char [strlen(otherString.string)+1];
      assert (string != NULL);
      strcpy (string, otherString.string);
   }
}


/////////////////////////////////////////////////////////////////////
// Function:  Display
//
// Prints the object's member data, string, to the screen.  If NULL
// pointer or length of string is zero, nothing will be displayed
//
//
// Inputs: Boolean TRUE or FALSE.
//
// Outputs: The string data member output to the screen; if input is
//          TRUE, a new line is output.
//
void String::Display(Boolean NewLine) const
{
      // if string is NULL pointer, print it
   if (string != NULL)
      cout << string;

      // if Boolean is TRUE, print a newline
   if (NewLine == TRUE)
      cout << endl;
}


/////////////////////////////////////////////////////////////////////
// Function:  countChars
//
// Counts the number of characters in the object's data member,
// string, and returns the number of characters.
//
// Inputs: None.
//
// Outputs: The number of characters of the object's data member,
//          string.
//
int String::countChars(void) const
{
      // if string is NULL pointer, return 0; else return the length
   if (string == NULL)
      return 0;
   else
     return strlen(string);
}


/////////////////////////////////////////////////////////////////////
// Function: countChars
//
// Counts the number of occurrences of the input character in the
// object's data member, string.
//
//
// Inputs: A character to be matched.
//
// Outputs: The number of occurrences of the input letter.
//
int String::countChars(char letter) const
{
      // if string is NULL pointer, return 0
   if (string == NULL)
      return 0;

      // if string not NULL pointer, loop through the array and
      // increment the counter when an element matches the
      // specified character; return the total number of matches
   else
   {
     int length = strlen(string);
     int letterCounter = 0;
     for (int i = 0; i < length; i++)
     {
	if (string[i] == letter)
          letterCounter++;
     }

     return letterCounter;
   }
}

Back to Outline