CS228 Fall 2005

Homework 2
Due Date: September 14, 2005
Last Submission Date: September 15, 2005 (25% off)

Objectives:

The concept of "string" as a sequence of characters has a long history in programming languages. In C, string wasn't even a built-in type; it was just a common name for an array of chars that contained at least one null character ('\0'). This is still the way that the C++ compiler handles strings that are hardcoded into the program (string literals or string constants).

Because they were not a built-in type, C (and early versions of C++) had limited support for strings. (Mainly, just support for reading them in and printing them out.) The compiler would not help a programmer by making sure that the array was the right size for the string, or that the null character was preserved as the string was used and changed. C programmers spent much time writing useful functions that manipulated null-terminated character arrays - comparing strings, concatenating strings, copying strings, searching for substrings, etc. Many of these functions were bundled into a string library in C (and C++), which still exists as the <cstring> library in modern versions of C++. (You can read about some of these functions in your textbook in Chapter 4 and in Appendix G, or also by typing "man string" at the command prompt on the CS dept. Linux computers.) However, even these library functions still required the programmer to do considerable work in making sure the arrays were the right size and that the null character was not accidentally overwritten or omitted.

Therefore, when the Standard Template Library was created for C++ in 1998, it specified a "string" class that overcame the problems with the old C-style strings. (The Standard Library version of string is what you are using when you include <string> at the top of your program, and declare variables of type string.) In particular, the STL string class had to always make sure that the array was large enough to hold all the characters and that a null character was present. Additionally, the string class took advantage of C++'s operator overloading feature; for example, you can compare instances of string using == instead of the strcmp() function used for C-style strings.

For this assignment, you will practice using dynamic arrays by writing your own version of the STL string class. To avoid confusion with the Standard Library string class, we'll call our version String (note the uppercase S). You'll need to download the String.h and the String.cpp files. You should put your implementations of the member and friend functions for String in the String.cpp file. (Don't change anything in String.h. If you want to write other functions to help you in implementing the ones listed, then put their prototypes and implementations in the places indicated in the .cpp file.)

You may not use the Standard Library string class to help you implement our String class. However, you may use the functions from the cstring library for C-style strings, if you wish (and if you make sure to meet the preconditions for those functions).

When comparing Strings, you should compare them based on their lexicographical ordering. Lexicographical ordering is similar to alphabetical ordering, except that upper and lowercase letters are treated differently and provisions are made for ordering symbols like ! and *. Lexicogrphic ordering is based on the character set of your machine, usually ASCII. (See Appendix A in your text for a listing of the ASCII ordering.) For example, under alphabetical ordering "ball" < "Bell" because the lowercase and uppercase B's are considered the same; under lexicographical ordering, "Bell" < "ball" because uppercase B comes before lowercase b on the ASCII table.

When using the >> operator to read in Strings, the operator should first skip over leading whitespace and then read in characters up to, but not including, the next whitespace character. For example, if the user types in "Hello world" and the program has a statement like "cin >> myString;", only the world "Hello" will be stored in myString and the next character to be read from the input would be the space between "Hello" and "world". You will need to use the peek() function for istream to test whether or not the next character is whitespace before including in your String. You may also find the isspace() function from <cctype> useful; type "man isspace" at the prompt to read more about it.

You should test your implementation of the String class thoroughly. Here is the start of a test file for the String class: hw2.cpp. This test file is a sample only. It does not fully test your implementation. You will have to edit and add to this code in order to test your String.cpp code; remember that testing your code thoroughly is part of the assignment.

To help you manage the compilation process when working with several files, you can download a Makefile for this assignment. The Makefile works with the UNIX make command, which figures out which files need to be compiled for each program. Once you have the Makefile downloaded to your directory with all the other files, you should be able to compile by giving the command "make".

Remember that part of the points for this assignment will be based on program documentation and coding style, as discussed in the first recitations.

When you are ready to submit your assignment,

After the script has executed you will receive and email message to your CS Unix account email verifying the electronic submission and listing the file(s) that you turned in. If no files are listed then no files were turned in! In other words, you did something wrong and have not turned your assignment in. Check to make sure what you want to turn in is indeed in your turnin directory. Make sure you run the turn in script from within your turnin directory. Make sure there are no typo's in your turn in command.

Go easy on the CS department's resources: After you have submitted your final version, you will probably have a number of .o, executable, and possibly core files in your ~/cs228/hw3 directory. These can take up a lot of disk space, so please go into your hw3 directory and remove them. As long as you leave the .h and .cpp files alone, you should be able to recreate the .o and executable files anytime you need them.