Computer Science 2611
Fall 2000
Laboratory Assignment 11

Handed out: 11/9/00

Note. Before doing this assignment you will need a working solution to assignment 10.

Keep in mind that your solution to assignment 10 is supposed to work for many different translation tables. To add another pair of words to the table should not require recompiling the program. So when you write the program, you do not know exactly what the translation file contains.

Your solution to assignment 10 defines a constant such as MAX_STRING_LENGTH for the number of characters that a string in the translation table can occupy. When a string is stored, the maximum number of bytes is used, regardless of how long the string is. Often, most of the space is wasted.

For example, suppose that MAX_STRING_LENGTH is chosen to be 100, to allow for long strings. To store the string "jerky", your program uses 101 bytes. Only the first 6 of those bytes will have useful information in them; the remaining 95 bytes are wasted.

It is possible to reduce the waste, using only as many bytes as are needed for each string. Instead of storing the strings as arrays inside the structure, the structure can be designed to hold pointers to the arrays. The type of a table entry can be defined as follows.

  struct TableEntry
  {
    char* realWord;
    char* rugratWord;
  }
The translation table is an array of table entries.

Write a function called readWord that reads a word from a given file. It should read that word directly into an array of size MAX_STRING_LENGTH + 1. But then it should allocate, using new, a new array that is just big enough to hold the string, copy the string into that array, and return a pointer to that array.

Read a line of the translation table by calling readWord twice, and installing the two pointers that are returned into the translation array.

When your program is done, it should return all memory that was allocated using new to the system. Draw a picture to see what the translation table looks like! Then think about what needs to be done to return the allocate memory to the system.

Test your program and after it is working turn it in in the usual way.