CMSC 15200 - Summer 2016

Homework #5: Due: Thursday August 11th, 2016 @ 11:59pm

In this assignment, you will get some practice with string operations, command-line inputs, and input/output redirection. You should submit one file, hw5/counter.c, in your subversion repository. Do not commit a.out to your repository; we will recompile your code on our own machines on our end (in fact, we must).

Word Counter

In this assignment, you will count the number of times a word appears in each sentence. The program will require the user to enter in the word as a command line argument. If user does not provide the word via the command line then the program will exit and print a usage statement (see below in the sample executions). The program will read in the text of sentences from a file by redirecting standard input.

Main Requirement: For this assignment, you need to read in the entire file into a string before printing out any information back to the user. Thus, you cannot print anything as you process the sentences.

You need to decide what is the best way to structure your program and store the counts. 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 counts in a specific manner).

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


Assumptions

  • The file will contain no more than 2048 characters. Note, there is no assumption about the length of a sentence.
  • Whitespace characters for this assignment: '\n', ' ' , '\t'
  • End of a sentence characters: '.', '?', '!'
  • The end of a word is marked by a whitespace character or an end of a sentence character.
  • You can assume the word will always be the first argument of the command line (but remember what's stored at argv[0]!).
  • You can assume that standard input will always be redirected from a file.
  • The words are case-sensitive (i.e., "like" and "LikE" are two different words).

Command Line Options

The program should also provide the following command-line arguments after the word to be counted.

The -l option prints the location of the word inside a sentence (i.e, the word is the first word in the sentence, and/or second word in the sentence, etc.).
The -t option prints the total number of times the word appeared in the file.

The user can provide zero or more command line options and they can specify the options in any order (again, after the word to be counted).


Sample Executions

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

Assume the following:

  • 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*vegetables*or*do*you*like*pizza?*I*like*dogs.*Cats*are*scary.

Executions

    If we compiled the program as such:
    username@hostname:~$ gcc -std=c99 -o counter counter.c
    username@hostname:~$
  1. Word provided, no options:
    username@hostname:~$ ./counter like < simple.txt
    Sentence #1 count = 1
    Sentence #2 count = 2
    Sentence #3 count = 1
    Sentence #4 count = 0
    username@hostname:~$

  2. No word provided, no options provided:
    username@hostname:~$ ./counter < simple.txt
    usage: counter word [-l] [-t]
    username@hostname:~$

  3. Word provided, option -l provided:
    username@hostname:~$ ./counter like -l < simple.txt
    Sentence #1 count = 1
    Sentence #1 locations = 2
    Sentence #2 count = 2
    Sentence #2 locations = 3, 8
    Sentence #3 count = 1
    Sentence #3 locations = 2
    Sentence #4 count = 0
    Sentence #4 locations = 0
    username@hostname:~$
  4. Word provided, option -t provided:
    username@hostname:~$ ./counter like -t < simple.txt
    Sentence #1 count = 1
    Sentence #2 count = 2
    Sentence #3 count = 1
    Sentence #4 count = 0
    Total = 4
    username@hostname:~$

  5. Word provided, options -t and -l provided:
    username@hostname:~$ ./counter like -t -l < simple.txt
    Sentence #1 count = 1
    Sentence #1 locations = 2
    Sentence #2 count = 2
    Sentence #2 locations = 3, 8
    Sentence #3 count = 1
    Sentence #3 locations = 2
    Sentence #4 count = 0
    Sentence #4 locations = 0
    Total = 4
    username@hostname:~$

    Helpful Links

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

    • You can use getchar: reads in a character from standard input
    • You may use fgets or getline if you wish.
    • string.h: functions for operating on strings in C. Take a close look at strcmp and strlen.

    Your File

    In order to compile your files, you will need to write a main function. You will need to use the "checkit_string" function on a variety of strings.

    Style

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

                  /* Jane Doe, jdoe */
                  /* CS152, Summer 2016 */
                  /* Homework 5 */
                  
    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/hw5/counter.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 the lecturer. We'll get you set up with a repository in time to make the deadline.