Handed out: 3/18/03
The Rugrats have a tendency to use incorrect words. For example, where they mean prosecutor, they say persecutor.
This assignment is to read a file and to replace each of a collection of words by rugrat-words. The table of rugrat-words is as follows.
Real word | Rugrat word | |
---|---|---|
prosecutor | persecutor | |
witness | witless | |
perpetrator | poopetrator | |
jury | jerky |
For example, if the input file contains
The prosecutor said, "Ladies and gentlemen of the jury . I would like to call my first witness , who will testify that the defendant is the perpetrator ."then the output will be
The persecutor said, "Ladies and gentlemen of the jerky . I would like to call my first witless , who will testify that the defendant is the poopetrator ."
The program should get the name of the input file from user. It should print the output to the standard output (cout). For example, a session might go as follows.
What file shall I read? test1.txt The persecutor said, "Ladies and gentlemen of the jerky . I would like to call my first witless , who will testify that the defendant is the poopetrator ."
Put the translations in your program using two arrays. The first contains the real words, and the second holds the rugrat words. Use parallel arrays. The idea is that rugratWord[k] is the rugrat word that corresponds to realWord[k]. Build the translations into your program. Here is a definition of the arrays. This definition uses type char* for the strings, which we have not discussed, but you will be able to treat realWord[k] or rugratWord[k] as an array. (realWord and rugratWord are arrays of arrays.) For example, realWord[0][0] = 'p', since 'p' is the first letter (index 0) of the array that is the first string in array realWord.
char* realWord[] = {"prosecutor", "witness", "perpetrator", "jury" }; char* rugratWord[] = {"persecutor", "witless", "poopetrator", "jerky" };
Normally, you have not been allowed to use any global variables. For this assignment, you can make these two arrays global. You can also make a variable that holds the sizes of these arrays (the number of words) global, but nothing else. Just declare them all before any functions, not inside a function.
Do not use operator == to test whether two null-terminated strings are the same string. Use function strcmp, from the cstring library. If A and B are null-terminated strings, then strcmp(A,B) returns 0 if A and B are the same string. It returns a negative number if A is alphabetically before B and a positive number if A is alphabetically after B.
Write a function called Translate that takes two parameters, each an array of characters. Translate should set the second array of characters to hold the translation of the first array. For example, Translate("jury", A) would store string "jerky" into array A.
If the string being translated is not one of the words in the realWord array, then Translate should set the output array to be a copy of the input string. (Use strcpy to copy.) For example, Translate("box", A) should copy "box" into array A.
Test your translation function. Try translating a few words, and see what you get.
Recall that you can read a file by creating an object of type ifstream and tying it to the file.
ifstream in; char filename[50]; ... in.open(filename);will open the file whose name is in array filename. It assumes that filename is a null-terminated string. The ... part needs to put something into the filename array.
For this program, a word is any sequence of characters that does not include any white-space characters (blanks, tabs or newlines). If in is an ifstream object that has been opened for input, then
in >> word;will read a string into array word, and will add a null terminator for you.
Write your main program to
Get the name of the file to read from the user.
Repeatedly
Keep going until an end-of-file is encountered.