Computer Science 202
Systems Programming Concepts Using C
Lab 5 - Scrambling and Unscrambling
Winter 1998

Overview

Write a C program using the GNU C Compiler (gcc) that will read in a text file and either scramble it or unscramble it.

Specifics

Your program will receive from the command line a series of arguments. The first will be a single character. If that character is an S then the file is to be scrambled, if the character is a U then the file is to be unscrambled. The next argument is the number of passes that are to be made through the file. The last two arguments are character strings. The first is the name of the input file and the second is the name of the output file.

When scrambling is to be done, your program is to read in the initial input file (using the read command) and write it out to the output file (using the write command). Your program will then make the designated number of passes through the output file to scramble it. On the first pass your program is to start with the first character and swap it with the second character, then take the third character and swap it with the fourth, until the end of the file. On the second pass your program is to start with the first character and swap it with the fourth character, then take the 5th character and swap it with the 8th, and so on until the end of the file. On a third pass your program is to start with the first character and swap it with the 6th character and so on. If there are extra characters at the end of the file that cannot be swapped, then they are printed as they are. The reverse is done for unscrambling.

For example consider the following line of text:

     There is no place like home. 

After the first pass the line should look like the following:

     hTre esin  olpca eilekh mo.e

After a second pass the line should look like the following:

     eTrhies o  napcllei  kheeo.m

Consider another example, if the file in.file contained the following text:

Row row row your boat
gently down the stream.
Merrily merrily merrily merrily
life is but a dream.
And the following command was issued: a.out S 4 in.file out.file

The file out.file would contain the following text:

oR owworor   yrwau
bgteodnytol   nhwrettms
eM.ea rlrmiry rleryeilm rmyei
rlrliey fiitsu  br edaa.m
If the following command were to now be issued: a.out U 4 out.file out2.file

The file out2.file would contain the following text:

Row row row your boat
gently down the stream.
Merrily merrily merrily merrily
life is but a dream.

Remember that spaces and carriage returns are characters also.

In class assignment

Demonstrate that your program correctly handles the list of command line arguments and does the appropriate error checking.

Deliverables

The following must be delivered to your instructor via text-only email no later than midnight of 6thMonday, February 9, 1998.
  1. Source code for your program.
  2. One set of input data.
In addition, you must deliver to the instructor at the start of class (Lab) on 6thTuesday, February 10, 1998, the following:
  1. A printed copy of your source code.

Notes

  1. Your solution must use the proper input and output commands.
  2. Your solution must include the proper use of the lseek command.