Lab 6: Command Line Processing

Due by the end of class

Introduction

For this lab, you will be implementing a program called wordlist that takes in command line arguments. The arguments will specify a sequence of actions to be performed on a specified list of words. The possible actions are backwards and reverse, which will be explained in detail later.

Description

Your program will implement the following commands, which will be passed on the command line. Don't let the notation scare you. It will be explained shortly.

-(b|r)+ sentence
sentence:

For this lab a word is any string containing only alphanumeric characters. The sentence is a list of words separated by whitespace. The sentence must be nonempty (that is, there must be at least one word in it). Any arguments following the commands (which always start with -) will be words.

b:

Command b represents the action backwards. When a b is recognized as a command by your program, each separate word should be converted to a backwards version of itself. Be careful not to confuse this command with r.

Usage

./wordlist -b hello friendly world

Output

olleh yldneirf dlrow
r:

Command r represents the action reverse. When an r is recognized as a command by your program, then the order of the words in sentence should be reversed. Be careful not to confuse this command with b.

Usage

./wordlist -r hello friendly world

Output

world friendly hello

More on the Command Line

Let's take another look at the command line options. They are expressed using a form of BNF grammar.

-(b|r)+ sentence

The plus sign ( + ) indicates that one or more repetitions of the commands are expected. The vertical bars ( | ) indicate that for each command, the user has a choice between b or r. Here are some examples of possible commands and their meanings.

-b
Apply backwards to the sentence, making each word backwards
-r
Apply reverse to the sentence, reversing the order of the words
-rr
Reverse the words and then reverse them again, which returns them to normal order
-br
Make each word backwards and then reverse the list of words
-bbb
Make each word backwards, and then backwards versions of that (making them normal again), and then backwards a final time (making them again backwards)

Note that actions are performed on the sentence in the order that they appear. You will need to loop through your command line arguments, look at each argument character by character, and perform an action to modify your sentence sentence as you encounter each action.

What You Have to Do

  • You must write code that parses through the command line arguments, performs actions on the specified sentence (in the order the actions appear), and then prints the modified sentence.
  • You may use string.h and stdio.h but not any other libraries. The string.h library will give you the strlen() function, which will allow you to find the length of a string. Use man pages for more information.
  • You must modify the sentence and then print its modified version, not simply print out arguments in reverse order or something similar.
  • Compile your program using a makefile.
  • Make your final executable called wordlist.
  • Commands to your program must come through the command line as show above. Your program does not use stdin for input. In other words, do not use getchar() or scanf().

Organization

You are free to implement this lab any way you like, but you may find it helpful to write a few helper functions.

// Makes each word from start up to but not including stop backwards. 
// Note that you will always start with index 2, since index 0 is the
// name of the executable and index 1 is the list of flags. 
// The stopping point is argc.
void backwards(char **argv, int start, int stop);

// Reverses the order of the words in the sentence from start up to
// but not including stop. Again, the list of words starts at index 2
// and ends at argc.
void reverse(char **argv, int start, int stop);
Also note that your normal int main() definition needs to be changed for this lab to the following.
// argc: number of command line arguments
// argv: array of strings (contains the command line arguments)
int main(int argc, char **argv)

Recommended Plan of Attack

  • First, take care of parsing through your command line arguments. The actions will be given by the first argument (in index 1) in argv.
  • Implement the helper functions corresponding to each action. Call them at the appropriate place in your code as determined in the previous step. Test each function separately as you go along.
  • Make sure that your program executes actions in the order given. Try some of the examples with multiple actions above like -br.

Turn In

Zip the contents of your lab directory, including the makefile and the source C file. Upload this zip file to Brightspace. Do not include any object files or executables. Running the make command must compile the required C source code file and generate an executable named wordlist.

All work must be done individually. Never look at someone else's code. Please refer to the course policies if you have any questions about academic integrity. If you have trouble with the assignment, I am always available for assistance.