A String Class: Part I - Lab

The Problem

Using arrays of characters to store strings in sufficient for basic operations performed on strings; however, there are problems that a simple array representation does not address. Here are some categories of problems, with an example for each:


Example: Because strings are stored in arrays, it is possible to attempt to access locations outside of the memory allotted to the string:
char label[10] = "value";
label[-1] = 'a';   // Eeek!
label[200] = 'z';  // No such location here!
Such attempts access memory that the code shouldn't, and may cause the program to crash.


Example: A string cannot easily grow. In other words, without doing some dynamic allocation, the maximum size of a string is predetermined:
char label[10] = "value";
// Not enough room, though it will attempt it anyhow!
strcpy(label, "How now brown cow?");


Example: Using library functions with strings is not terribly natural:
if (strcmp(label1, label2) > 0)  // Ugh!
  strcpy(label1, label2);
It would be more natural to be able to do:
if (label1 > label2)  // Ahhhh...
  label1 = label2;


Example: Determining the length of a string is inefficient in that it takes more time to find the nul character (\0) the longer the string:
char name[] = "my name";
char dict[] = <Insert the contents of the dictionary here>;

cout << "My name is " << strlen(name)  // Fast
     << " characters long." << endl;

cout << "The dictionary is " << strlen(dict)  // La, la, la...
     << " characters long." << endl;

The Solution

In this lab exercise, we will attack specific problems of:

We'll leave the other problems to a future exercise.

A Class

C++ allow us to create new data structures (i.e., something with data and functionality) and wrap it up into a neat package called a class:
class String {
  //  - allowing a user of the string to set up, access or
  //    change the string.

  // DATA
  //  - needed to store the contents of the string and any other
  //    values that assist in our representation of the string.
By defining such a String class, we can solve our two problems:

Your Task

You must write a String class that performs some range checking and efficient determination of length, and that provides some other basic functionality.

Your String class will have the following design:

  1. Modularity

    The String class code must be put in its own module, i.e., a file mystring.h (the header file) should hold the class definition and a file mystring.cpp (the implementation file) should hold the method definitions.

  2. Data

    Each String object should contain the following data as part of its internal representation:

  3. Functionality

    The String class should provide the following methods for setting up, accessing and changing strings:

In your implementation of String's methods, you may use the library functions provided by string.h, which operate on arrays of characters terminated by nul's (\0).

A Test Program

The file String-test.cpp provides a main program to test your new class.


In order to demonstrate an understanding of how to create and use a String class to represent strings, you should be able to define the following terms:

BU CAS CS - A String Class: Part I - Lab
Copyright © 1993-2000 by Robert I. Pitts <rip at bu dot edu>. All Rights Reserved.