Computer Science 2611
Spring 2003
Programming Assignment 8

Handed out: 3/25/03

The assignment

For this assignment, modify your solution to assignment 7 in the following four ways. Turn in only the final versions, with all of the modifications made.

Modification 1

Assignment 7 has you build the words into the program. As a consequence, if you want to change the real words and rugrat words, you have to modify the program and recompile it. A more convenient approach is to put the words into a file, and let the program read the file. That way, to modify the words, you do not need to modify and recompile the program.

The translation data file should look like this.

 prosecutor	persecutor 
 witness    	witless   
 perpetrator	poopetrator
 jury		jerky
Write a function that reads the translation file and stores its data into an array of structures. Each member of the array should be a structure holding two strings (arrays of characters), the real word and the rugrat word. You can add additional translations if you like.

Important Requirement. Your program must not presume that there are only 4 pairs of words in the translation file. It must be possible to add new words without modifying the program. Make your file reader count the number of pairs of words.

Modification 2

Assignment 7 has you put the translations into parallel arrays. Modify that to use an array of structures. Make a single translation array, with each one holding a structure of two arrays of characters.

Modification 3

Assignment 7 allows you to use global variables for the arrays realWord and rugratWord. For this assignment, you will not be allowed any global variable at all. Instead, pass the arrays among the functions that need them as parameters.

Modification 4

Assignment 4 has you read strings that are separated by white space. That means you cannot recognize words with punctuation around them. Modify that to treat punctuation as not being a part of a word. Write two functions.

  1. ReadNonword(in,arr) reads as many nonletters from ifstream object in as it can, up to a letter or an end-of-file. It stores them into array arr, as a null-terminated string. Store all of the characters, including blanks and newlines.

    Just use a loop, reading one character at a time. Use peek to look at the next character. Keep going as long as the current character is not a letter.

    If you include <ctype> or <ctype.h>, then you can use function isalpha(ch) to test whether ch is a letter.

    Make ReadNonword return 1 if it got at least one character from the file, and 0 if it encountered an immediate end-of-file.

    If the first character is a letter then set arr to hold an empty string, and return 1. Only return 0 when an end-of-file is immediately seen.

  2. ReadWord(in,word) reads a word consisting of letters, and stores it into array word as a null-terminated string. It should continue reading until it sees something other than a letter (or the end of the file).

    Make ReadWord work similar to ReadNonword. In fact, ReadWord and ReadWord will be almost identical. You can implement them both using one helper function if you see how to do that. But a simple approach is just to make a copy of ReadNonword and edit the copy. Be sure to test one of the functions before making a copy and editing the copy. It is a waste of time to make a copy of an incorrect function definition.