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).
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.
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, "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 ==.
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.
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.
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.
Keep going until an end-of=file is encountered.
Be sure to test your program before turning it in.