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

CS 19 Fall 2018

Assignment 3 (Table of Contents)

Filename

content.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. Input is terminated with EOF (ctrl-D). You may assume that, at most, 10,000 unique words will be entered. There is no limit to the total number of words input. You are required to use arrays for this assignment, and may not use STL Containers (such as the Vector or Map—you will have a chance to use them on a future assignment.) (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 that is in the interior (or start) of a word. (example: quote." will be recorded as the same word as quote, but it's and its would be recorded as different words) You will discard the case of the input, storing and outputting the results in all lowercase letters.

When the input is completed, your program should print out the words, one per line in alphabetical order. You are required to use either of the sorting routines that we have discussed in class to perform this sorting. 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 must have no other output.

Since we haven't discussed data objects yet, the easiest way to store the words is to use a string array for the words (For this assingment you shoud use C++-style strings, and not c-style character arrays) and an int array to store the count of each word.
Before you add words to your list, your 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. 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. You should expect to have about six functions in the finished program.

Suggested order for writing this program:

  1. input multiple lines of text - termiated with EOF
  2. break each line into seperate words
    (or read each word individually and skip step 1)
  3. check to see if a word is already in the array before you add it
  4. sort the words (and counts!) in the array before output

What to turn in

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

 

Sample Input1

"We weaken our greatness when we confuse our patriotism with tribal
rivalries that have sown resentment and hatred and violence in all the
corners of the globe. We weaken it when we hide behind walls, rather
than tear them down; when we doubt the power of our ideals, rather
than trust them to be the great force for change they have always
been.

We are 325 million opinionated, vociferous individuals. We argue and
compete and sometimes even vilify each other in our raucous public
debates. But we have always had so much more in common with each other
than in disagreement. If only we remember that and give each other the
benefit of the presumption that we all love our country, we will get
through these challenging times. We will come through them stronger
than before, we always do."

(matching) Sample Output

"we 1
325 1
all 2
always 3
and 5
are 1
argue 1
be 1
been 1
before 1
behind 1
benefit 1
but 1
challenging 1
change 1
come 1
common 1
compete 1
confuse 1
corners 1
country 1
debates 1
disagreement 1
do 1
doubt 1
down 1
each 3
even 1
for 1
force 1
get 1
give 1
globe 1
great 1
greatness 1
had 1
hatred 1
have 3
hide 1
ideals 1
if 1
in 4
individuals 1
it 1
love 1
million 1
more 1
much 1
of 3
only 1
opinionated 1
other 3
our 5
patriotism 1
power 1
presumption 1
public 1
rather 2
raucous 1
remember 1
resentment 1
rivalries 1
so 1
sometimes 1
sown 1
stronger 1
tear 1
than 4
that 3
the 6
them 3
these 1
they 1
through 2
times 1
to 1
tribal 1
trust 1
vilify 1
violence 1
vociferous 1
walls 1
we 12
weaken 2
when 3
will 2
with 2

What to "turn in"

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


1. from John McCain's Final Letter to America