CIS-3012 Homework #3: Strings

Due: Wednesday, March 8, 2017

To support this assignment the primary reading in the text is...

Although the C++ standard library contains a string class, it is instructive to write your own. In this assignment you will create a basic string class that allows strings to grow dynamically to any size (similar to std::string).

The attached file, String.hpp, contains a definition of a very minimal string class. A few of the methods are already implemented inline. For this assignment you are to implement the remaining methods in a String.cpp file.

In a manner similar to BigInteger these strings are intended to hold a pointer to a dynamically allocated array of characters containing the text of the string. Copying and assigning string objects should copy this array as needed. When characters are added to the string via the insert method, the array may need to be reallocated to increase its size.

Reallocating the internal array for every insert operation is inefficient. For this assignment, you should maintain a larger array than necessary and only reallocate when the "extra" space fills up. You will need to track the size of the array and the size of the string separately since not all positions in the array are really being used by the string. Also when doing a reallocation you should double the size of the array. This approach provides amortized constant time appends and is standard (you can be sure std::string is doing something similar).

Submit String.hpp and String.cpp in a zip archive. Be sure to include your name in both files. You should submit the header file because you will likely need to make changes to the class's private section. I encourage you to write a demo/test program to exercise your class, but you don't need to submit it.

Last Revised: 2017-02-15
© Copyright 2017 by Peter C. Chapin <>