CMSC 15200 - Summer 2013

Homework #4 (Long): Due: Friday August 16th, 2013 @ 1:30pm

In this assignment, you will get some practice with string operations, dynamic memory allocation, command-line inputs, and input/output redirection. There are no new concepts in this assignment. The difficulty is that you will be given fewer step-by-step instructions.

The Modifier

You will create a program called modifier. This program will read in text from a file by redirecting standard input and modify the text based on the options given to the program (via the command line). After modifying the text, the program will print it to the screen.

You will place your code in a file called modifier.c

There will be no more than 50 sentences in a file. You can also assume while reading in the file that each sentence will have no more than 100 characters. But a sentence can contain more than 100 characters after modifiying it.


Requirements

The main requirment for this assignment is that you need to read in the entire file before performing any modifications to the file. The file's text also needs to be represented as string(s).

You need to decide what is the best way to structure your program and store the text. Leave comments as you are implementing the program (i.e., reasons for how you're structuring the program or the reason why you decided to store your text in a specific manner).

A newline shoule be printed before the exiting of the program.


Command Line Options

The program will modifiy the text in the file based on the options the user gives on the command line. The options and their descriptions are listed below.

-i The -i option modifies the text by adding 4 spaces to the beginning of each sentence. A sentence will always end with either a (period .) , (exclamation point !), or (question mark ?).
-n The -n option modifies the text by adding (i.e. appending) a newline character to every sentence in the file. A sentence will always end with either a (period .) , (excalmation point !), or (question mark ?).
-t INTEGER The -t option replaces all tab characters (i.e., '\t') in the file with INTEGER amount of spaces. E.g. -t 10 will replace each occurance of a '\t' character with 10 spaces.
-w WORD (Challenge Option: Not Required. 10% Extra Credit)The -w option removes the given WORD from the text of the file. If the WORD was not found in the file then the modified text should be unchanged.The whitespace that comes before/after should not be affected by removing the word. This option is not case sensitive. A sentence will always ends with either a (period .) , (excalmation point !), or (question mark ?).

The user can provide zero or more command line options and they can specify the options in any order . You can assume that standard input will always be redirected to a file.


Sample Executions

This section will give you some sample executions of the modifier program.

Assume the following:

  • The $ represents tab spaces in the examples. It is hard to differentiate between spaces and tabs on a web page so we will just say $ is a tab. DO NOT LOOK FOR OR INSERT $s IN YOUR PROGRAM ! This is only for visual purposes.

  • The * represents spaces in the examples. DO NOT LOOK FOR OR INSERT *s IN YOUR PROGRAM ! This is only for visual purposes.

Lets say we have file called simple.txt that has the following text:

I*like*food.*$Do*you$like*food?*I*like*dogs.*Cats*are*scary.

Exeuctions

    If we compiled the program as such:
    doe@fafner:~$ gcc -o modifier modifier.c
    doe@fafner:~$
  1. Option: -i
    doe@fafner:~$ ./modifier -i < simple.txt
    ****I*like*food.*****$Do*you$like*food?*****I*like*dogs.*****Cats*are*scary.
    doe@fafner:~$
    Notice how at the start of every sentence (except the 1st) there are actually five spaces. Once space was for the space that was previously there followed by the additional four spaces. Adding additional spaces should not affect any spaces that come before it.

  2. Option: -n
    doe@fafner:~$ ./modifier -n < simple.txt
    I*like*food.
    *$Do*you$like*food?
    *I*like*dogs.
    *Cats*are*scary.

    doe@fafner:~$

  3. Option: -t INTEGER
    doe@fafner:~$ ./modifier -t 2 < simple.txt
    I*like*food.***Do*you**like*food?*I*like*dogs.*Cats*are*scary.
    doe@fafner:~$
  4. Option: -w WORD
    doe@fafner:~$ ./modifier -w liKE < simple.txt
    I**food.*$Do*you$*food?*I**dogs.*Cats*are*scary.
    doe@fafner:~$

  5. Options: -n -t INTEGER
    doe@fafner:~$ ./modifier -n -t 2 < simple.txt
    I*like*food.
    ***Do*you**like*food?
    *I*like*dogs.
    *Cats*are*scary.

    doe@fafner:~$

  6. Options: -n -t INTEGER -i
    doe@fafner:~$ ./modifier -n -t 2 -i -w doe< simple.txt
    ****I*like*food.
    *******Do*you**like*food?
    *****I*like*dogs.
    *****Cats*are*scary.
    doe@fafner:~$

  7. Helpful Links

    You might find the following functions/libraries helpful when implementing this program.

    • getchar : reads in a character from standard input
    • string.h : functions for operating on strings in C.
    • atoi : converts a string to int

    Style

    At the top of your C file, write a comment with your name, etc., in the following form:

                  /* Jane Doe, jdoe */
                  /* CS152, Summer 2013 */
                  /* Homework 4: Modifier */
                  
    This information is not strictly necessary, since your files are already identified by their names and the repository they reside in. Nevertheless, the redundancy is a helpful convenience for us when we are browsing and/or grading your work.

    Comments, where they occur, should be helpful and precise. Omit superfluous comments:

                  int a = b + c; /* I'm adding b and c and naming it a! */
                  
    Yes, we can see that.

    Your code should be no more than 80 columns wide.

    Do not write more than one statement on a line.

    Do not submit code that does not compile. If a function is broken, and makes compilation impossible, comment out that function and submit the rest. Non-compiling code will receive little to no credit.


    Submitting Your Work

    Save and commit your code in YOUR-REPOSITORY/hw4/modifier.c. Recall that you will need to add your work before you commit it. (Also, notice that in the -m message you include at commit time, -m is simply a command-line option.)

    Commit your work early and often. We do not grade intermediate commits, only the work as it stands at the deadline. If you have any issues with subversion, not only your instructors but your classmates can help you. Most of the students in this class have at least one full quarter of experience running subversion.

    If, for any reason, perhaps due to a late add, you do not have a CS repository, save your work somewhere you can easily get it and send mail to Adam. We'll get you set up with a repository in time to make the deadline.