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 the command line, rather than asking the user for it. It should print the output to the standard output (cout).
The translation data should be put into a file. It should look like this.
prosecutor persecutor witness witless perpetrator poopetrator jury jerkyWrite a function that reads this 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.
Write a function called Translate that takes four parameters: the array of translations; the size of the array of translations; and two arrays of characters. Translate should set the second array of characters to hold the translation of the first array. For example, if array translations is the array constructed in the preceding step, then Translate(translations, 4, "jury", A) would store string "jerky" into array A.
If the string being translated is not one of the real words in the translation table, then Translate should set the output array to be a copy of the input string. (Use strcpy to copy.) For example, Translate(translations, 4, "box", A) should set array A to hold string "box".
Test your translation function. Try translating a few words, and see what you get. If it does not work, try printing the translation table so see that it is correct.
Just do a linear search in the array to find the string, if if it there. Be sure to use strcmp to compare strings. Do not compare strings using ==.
Your program needs to read words from the input file. If you use >> to read the words, you will get punctuation as well as letters, and your program will not work. Write two functions
ReadNonword(ins,arr) reads nonletters from ifstream object ins and stores them into array arr, as a null-terminated string. It should continue reading and printing until it sees a letter, or the end of the file. Just use a loop, reading one character at a time. Use peek to look at then next character.
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, not the end-of-file, then set arr to hold an empty string, and return 1. Only return 0 when an end-of-file is immediately seen.
ReadWord(ins,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 similarly 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. Otherwise, just write and test one of them, then make a copy of that one and modify the copy.
To implement these two functions, you will need the following methods that are supported by ifstream objects, as well as a classifier of characters.
ins.peek() returns the next character that would be removed from input stream ins, if it were read. But peek does not remove that character from the stream. Several peeks in a row will produce the same character. This allows you to check to see whether the next character is a letter without consuming it from the stream. If there are no more characters in the file, then ins.peek() returns the constant EOF. (Note: peek returns an integer, the Ascii code of the character. EOF is -1.)
ins.get() reads (and removes) a single character from input stream ins, and returns it.
isalpha(c) returns a nonzero value if c is a letter, and returns 0 otherwise. You need to include header file ctype.h to use this.
Up to now, we have been using a main program without any parameters. In reality, the operating system sends some parameters to main. The heading for main is
int main(int argc, char* argv[])where argc is the number of arguments on the command line (including the command line itself) and argv is an array of strings that holds the parts of the command line. For example, suppose you compile your program into file a.out, and you type command
a.out myfile.txtThen when the program is called, main is called with argc = 2 and argv holding two strings: argv[0] is "a.out" and argv[1] is "myfile.txt".
So to get the name of the file to open, just get argv[1]. Be sure to check that argc is 2, since otherwise the program is being used incorrectly.
A value of type char* is very similar to an array of characters, and you can think of it as an array of characters. It is a null-terminated string.
Write your main program to
Get the name of the file to read from, and open it for reading;
Repeatedly
Keep going until an end-of=file is encountered.