This exercise uses arrays of characters, which are used to store strings of characters.
For this assignment, a "word" is any sequence of characters that does not contain a blank or newline character. For example, "r92#sm" is a word.
You should not have to tell this function how long the string is, since it can find out for itself. Function strlen computes the length (logical size) of a null-terminated string. Write strlen(s) for the length of the string stored in array s. Be sure to include system header file string.h to use strlen.
Write a small main program that tests this function. An easy way to test it is to give it constant strings. For example, if your function is called PrintBackwards, then try writing
PrintBackwards("abcde");
Test your function before moving on.
Write a program that starts by reading the names of two files from the user: an input file name and an output file name.
Read each file name into an array of characters. If you read an array of characters using an ifstream object such as cin, a string will be read up to a blank or end-of-line. (But see footnote.) So if you write
char infilename[MAX_FILE_NAME_SIZE]; cin >> infilename;then the name of a file will be read from the user, and stored in array infilename. You will need to define constant MAX_FILE_NAME_SIZE.
ifstream ins(infilename);then object ins is tied to the file whose name is stored in array infilename. Anything that you read from ins comes from that file. Use ins like you would cin.
Note: When you open a file, it is a good idea to check whether the file was opened successfully. You can use the fail operation for that as well. So to open a file for reading, write
ifstream ins(infilename); if(ins.fail()) { ... what to do if filename could not be opened for reading. }
The program should read words from the input file one at a time into another array of characters. After each word, that word should be printed backwards to the output file.
You can read the word using >>, just as you did for the file name.
Put a space between adjacent words.
The program should stop when it reaches the end of the input file. You can detect the end of file by asking the ifstream object whether it has failed on the previous attempt to read. Write
if(cin.fail()) { ... }to ask the cin object whether it failed on the previous attempt to read. (Of course, you won't be asking cin if it failed. Who do you want to ask?)
When the program finishes, it should close both the input and the output file.
Test your program before moving on.
As written, your program puts all of the words on one line. For this step, you will keep the lines reasonably short.
Keep track of the number of characters printed on each line. After each word, check whether you have printed at least 50 characters. If so, start a new line of output. That way each line will only have a little more than 50 characters on it.
Do not print an end-of-line character in the middle of a word. Break the lines between words.
For example, if the file contains
T'was brillig and the slithy toves Did gyre and gimbol in the wabe. All mimsy were the borogoves, And the mome raths outgrabe.then the following would be printed.
saw'T gillirb dna eht yhtils sevot diD eryg dna lobmig ni eht .ebaw lla ysmim erew eht ,sevogorob dnA eht emom shtar .ebargtuo
Be sure to count the spaces as part of the 50 characters.
Test your program on a file that is long enough to force a few lines of output to be printed.
Turn in a printout of your program.
Here are "some words"it will print
ereH era sdrow emossince it will treat "some words" as a single word.