Introduction
Counting words is the "hello world" of text processing and a perfect excuse to learn std::map. You will normalise messy real-world text (capitals, punctuation, apostrophes), count with a map, then re-sort the results by frequency — the same pattern used in search engines and log analysers.
Problem statement
Objectives
- Count occurrences with std::map<std::string, int>
- Use std::set for fast membership tests
- Copy map entries into a vector and sort by a different key
- Handle characters safely with <cctype>
Features
- Works on any text piped or typed in
- Keeps don't and well-known as single words
- Top-10 list with proportional bar charts, ties broken alphabetically
- Stop-word list kept separate from the ranking
Concepts demonstrated
- std::map
- std::set
- Structured bindings
- Custom sort order
- String normalisation
System architecture
Each line is split on whitespace; every token goes through normalise, which keeps letters and digits (lower-cased) and allows ' or - only after the word has started, then trims them from the end. Words are counted in an ordered std::map, which also gives alphabetical iteration. For the ranking, non-stop-words are copied into a vector<pair<string,int>> and sorted by count descending, then word ascending. Bars are scaled so the most frequent word gets 30 stars.
Folder structure
Complete source code
Makefile
What each file does
- src/main.cpp
- The stop-word set,
normalise, the counting loop, and the statistics and ranking output inmain. - Makefile
- Build automation:
makebuilds./app,make testruns the sample session and compares it with the expected output,make cleanremoves build files.
How to run it
Build
Run
Test against the expected output
Expected output
Generated by running the project with tests/sample-input.txt. Your output should match tests/expected-output.txt exactly.
C++ is a general-purpose programming language. It was created by Bjarne Stroustrup. The language supports procedural, object-oriented and generic programming! Programmers like C++ because it is fast, and because the standard library is rich. Don't worry: learning a language takes time. Practice, practice, practice.
Testing
- Pipe the sample text:
./app < tests/sample-input.txtmust matchtests/expected-output.txt. - Note that 'C++' becomes 'c': punctuation is not part of a word in this program.
- Run it on a large file (a book from Project Gutenberg) — it should finish instantly.
Common errors
Crash or assertion in isalnum with accented letters
Cast char to unsigned char before calling <cctype> functions, as normalise does.
Nothing happens when typing text
The program reads until end of file: press Ctrl+D (Linux/macOS) or Ctrl+Z then Enter (Windows).
Possible improvements
- Accept file names on the command line and process several files
- Let the user choose N for the top-N list
- Count two-word phrases (bigrams)
Additional challenges
- 1Replace std::map with std::unordered_map and sort the output yourself — what changes?
- 2Add stemming so 'programming' and 'programmers' count together.
- 3Print the words that appear in exactly one line.
Built and tested it yourself? Mark it to update your dashboard.