Computer Science 2610
Fall 2000
Laboratory Assignment 8

Handed out: 10/12/00

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.


Step 1

Write a function that takes an array of characters holding a null-terminated string as a parameter. It should print the characters in that string backwards. For example, if given string "abcd", it should print dcba.

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, excluding the null character) 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");
to test it.

Test your function before moving on.


Step 2

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 white-space character such as 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.

Test this by having the program echo the names of the files that it read, so that you can see that they are being read correctly.


Step 3

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. For example, if you write
  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.
    }

Step 4

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 in the output file.

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(ins.fail()) {
      ...
    }
to ask the ins object whether it failed on the previous attempt to read.

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

Test your program before moving on.


Step 5

As written, your program puts all of the words on one line. For this step, you will maintain the line breaks that were present in the input file.

Write a function called SkipWhite. Running SkipWhite(ins) should cause white space (blanks, tabs and newline characters) to be read from file ins, until either the end of the file is reached or a nonwhite character is seen. Then SkipWhite should return the number of line-break characters that it read.

To do this, you will need to be able to look at individual characters in the input. Use the following operations and constants.

  1. ch = ins.peek() will put into char variable ch the next character that would be read from ins, but will not consume it from the input. If you do several peeks in a row, you will get the same character each time. If there are no more characters in the file, then ins.peek() will return the special value EOF.

  2. ins.get() is similar to ins.peek(), but it skips over the character that it reads. If you do several get's in a row, you get successive characters of the file.

  3. You will need to use the blank (' '), tab ('\t') and line break ('\n') characters.
So to skip white space, use a loop that repeatedly peeks at the next character, and skips over it if it is a blank, tab or line break character.

Now modify your program so that, before reading each word, it uses SkipWhite to skip over white space. It should look at the result of SkipWhite, and print as many line breaks as SkipWhite skipped over before handling the next word. If there were no line breaks, then just print one space.

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 written into the output file.
saw'T gillirb dna eht yhtils sevot
diD eryg dna lobmig ni eht .ebaw

lla ysmim erew eht ,sevogorob
dnA eht emom shtar .ebargtuo

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.


Footnote

When you read a string, normally the >> operation
  1. Skips any leading white space (spaces, tabs and newlines).
  2. Reads up to a white space character or end of file.
But if, after skipping leading white space, it sees a double quote mark ("), then the >> operator reads a string up to the next double quote mark, and puts the string that is between the quotes into the array. So if your program reads
  Here are "some words" 
it will print
  ereH era sdrow emos
since it will treat "some words" as a single word.