copyright Steve J. Hodges   http://steveh.net/cs19/cs19-hw02.html

CS 19 Fall 2017

Assignment 2 (Word Counter)

Filename

wcount.cpp

Program Description

In this assignment, you will read an unspecified number of lines of text from STDIN. Each line will contain an unspecified number of words. You may assume that no line will be greater than 255 characters long. Input is terminated with EOF (ctrl-D). You may assume that, at most, 5000 unique words will be entered and that no word will be more than 15 characters long. (A word, here, is defined as a sequence of alphanumeric characters and punctuation delimeted by whitespace.) Your program should discard punctuation that is found at the end of a word, but preserve punctuation inside a word. (example: blue: is the same word as blue, but isn't is not the same as isnt) You do not need to preserve the case of the input words, but words need to match in a case-insensitive manner. (Suggestion: lowercase all of the words.)

When the input is completed, your program should print out the words, one per line in alphabetical order. Each word should be followed by an integer count of the number of times that word appeared in the text. (Use a single space to separate the word and the count.) (Your program should have no other output.)

Use arrays for this assignment. Use of Vector is not allowed.

Strings

For this assignment, you may use either C style strings (character arrays) or C++ style strings (string class.) Please use whichever you are more familar with, and use the String class if you don't have a clear preference. You are required to choose one or the other—don't write a program that uses them both for significant portions of the code. Both types of strings are described in chapter 9 of our text. If you use character arrays, the functions strcmp(), strcpy(), strtok() and strlen() will be helpful for this assignment. If you use C++ style strings, you may find the palindrome example in chapter 9 is helpful.

To store your words and counts, you are requird to use arrays. The easiest way to store the words is to use one array for the words and a second array to store the count of each word.

Before you add words, you should check to see if the new word is already in your array of words. Use an integer variable to keep track of how many words are stored in the array.

The sorting performed on the words must be performed by a sorting function that you code. You are allowed to use either of the two sorting routines disucssed in class. Sorting is an expensive operation, so only invoke sort once during your program run.

You may find it helpful to write a function that adds word to your array or increments the count of words that are already in the array. You may also find it helpful to write a function that looks up a word in your array.

What to turn in

As usual, leave your .cpp file for this program ("wcount.cpp") in your home directory on pengo.

 

Sample Input1

This may be the way the world ends not with
a bang
but with a temper tantrum.
Okay, a temporary government shutdown
which became almost inevitable
after Sunday's House vote to provide government

funding only on unacceptable conditions
wouldn't be the end of the world.
But a United States government default,
which will happen unless Congress raises
the debt ceiling soon,
might cause financial catastrophe.

Unfortunately, many Republicans either don't
understand
this or don't care.

(matching) Sample Output

a 4
after 1
almost 1
bang 1
be 2
became 1
but 2
care 1
catastrophe 1
cause 1
ceiling 1
conditions 1
congress 1
debt 1
default 1
don't 2
either 1
end 1
ends 1
financial 1
funding 1
government 3
happen 1
house 1
inevitable 1
many 1
may 1
might 1
not 1
of 1
okay 1
on 1
only 1
or 1
provide 1
raises 1
republicans 1
shutdown 1
soon 1
states 1
sunday's 1
tantrum 1
temper 1
temporary 1
the 5
this 2
to 1
unacceptable 1
understand 1
unfortunately 1
united 1
unless 1
vote 1
way 1
which 2
will 1
with 2
world 2
wouldn't 1

1. Paul Krugman, "Rebels Without A Clue", The Opinion Pages, New York Times online, September 29, 2013