Other Solutions to Lab #3

Back to the solution.

void String::Reverse(void)
{
  if(string)
  {
      // Determine length of string
    int i = strlen(string);  
    for(int index = 0; index < i/2; index++)
    {
        // Swap the values between 
        // index and (len - 1 - index)
      char temp = string[index];
      string[index] = string[i - 1 - index];
      string[i - 1 - index] = temp;
    }
  }
}

String String::ReverseNew(void) const
{
    // create a local object of type String and initialize with the
    // private data
  String strLocal(string);

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

  return strLocal;
}

Boolean String::isEqual(String otherString) const
{
    // if both strings are non-NULL, compare them
  if (string != NULL && otherString.string != NULL)
  { 
    if (strcmp(string, otherString.string) == 0)
	  return TRUE;
	else
	  return FALSE;
  }
  else
     // they either is NULL, they are equal iff both are NULL
   return (string == NULL && otherString.string == NULL ? TRUE : FALSE);
}

void String::Display(Boolean NewLine) const
{
  if (string)
    cout << string;
	
  if (NewLine)
    cout << endl;
}




Back to Outline