Computer Science 2611
Spring 2003
Laboratory Assignment 10

Handed out: 5/15/03

Note. This is a short and easy assignment. But before doing this assignment you will need a working solution to assignment 8.

Part 1

Your solution to assignment 8 needs to choose a maximum word length when it reads in the table of translations. It allocates that many bytes for every word in the translation table, even if the word is actually smaller. If some of the translation words are long, but there are many short words, this wastes a lot of memory. This assignment has you reduce the memory requirements.

Your translations are stored in an array of structures, where each structure holds a pair of words. Modify the structure that holds a pair of words so that, instead of holding a pair of arrays, it holds a pair of pointers. When you read in the translations, read each word into a temporary array whose size is the maximum allowed for words. But after reading the word into the temporary array, allocate an array in the heap whose size is just large enough, and copy the word into that array. Store a pointer to the newly allocated array into the translation table.

Write a function that makes a copy of a null-terminated string. If it is called duplicate, its heading should be

  char* duplicate(const char* s)
It should allocate strlen(s) + 1 bytes for the copy array, copy s into the new array, and return the address of the new array. Use function duplicate to create the pointer that is put into the translation table.

Part 2

Your program should have been designed to allow the file that contains the translations to be changed without making a change to the program. Add at least three more translations to that file. If you had to make a change to your program because of this, you have made a mistake in your program.

Test your program before turning it in.