out: Monday, May 5, 2025
due: Wednesday, May 7, 2025 by 10:00 am,
No late exercises accepted.
Goals: Write a small program using the C++ STL map
container and learn how to use C++ streams to read data from a file.
Description: Write a C++ program that reads a text file whose filename is given as a single command-line argument. The program should read and count the individual words in the file, and, after reading the entire file, it should print a list of the words sorted by the words, and the number of times each word appears in the file. Each word should be written on a separate output line with the word appearing first followed by a single space and the number of occurrences.
For example, if the file quotes.txt
contains
then the output ofto be or not to be to do is to be to be is to do do be do be do
./ex12 quotes.txt
should begin with the following:
be 6 do 5 is 2 ...
You should make the following assumptions:
>>
operator to read the input file one
word (string) at a time. You may assume that each string read by
>>
is a "word" for the purposes of this exercise -- you
do not need to do any additional processing to handle
whitespace, punctuation, or similar issues.word
, Word
, and woRD
are three different words
because they contain different lower- and upper-case letters.<
) relation for strings.Hints: Take advantage of the C++ STL library; one of the map
containers should be particularly useful.
For reading files, take a look at the ifstream
class.
Your code must:
ex12.cc
and compile without errors or warnings on CSE Linux machines
(lab workstations, attu, or CSE home VM) using the command
g++ -Wall -g -std=c++17 -o ex12 ex12.cc
const
and so on must make us
smile rather than cry. (Hints: Google C++ Style Guide, cpplint
)You should submit your exercise using the Gradescope dropbox linked on the course resources web page.