Computer Science 2610
Spring 2000
Laboratory Assignment 8

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.

Write a program that starts by reading the names of two files from the user. 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. So you write

   char filename[FILE_NAME_SIZE];
   cin >> filename;
The two file names are an input file name and an output file name.

Next the program should open the input file, creating a new ifstream object, and also open the output file, creating a new ofstream object. Just use the array names as the names of the files to open. The array of characters will be treated as a string.

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. But then you need to find out how long the string is. Function strlen will do that. Expression strlen(s) gives the number of characters in string s. To use strlen, include header file string.h.

To print a word backwards, just loop backwards across the array. Put a space between adjacent words.

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.

For example, if the file contains

T'was brillig and the slithy toves
did gyre and gimbol in the wabe.
then the following would be printed.
saw'T gillirb dna eht yhtils sevot did eryg dna lobmig 
ni eht .ebaw

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.

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, write

    in.open(filename);
    if(in.fail()) {
      ... what to do if filename could not be opened for reading.
    }

When the program finishes, it should close both the input and the output file.

Turn in a printout of your program.