Previous fileTop of DocumentContentsIndex pageNext file
Apache C++ Standard Library User's Guide

8.3 Example Program: A Spelling Checker

A simple example program that uses a set is a spelling checker.

NOTE -- This program can be found in the file spell.cpp.

The checker takes as arguments two input streams: the first represents a stream of correctly spelled words (that is, a dictionary), and the second a text file. To begin, the dictionary is read into a set. This is performed with a std::copy() function and an input stream iterator, copying the values into an inserter for the dictionary. Next, words from the text are examined one by one, to see if they are in the dictionary. If they are not, they are added to a set of misspelled words. After the entire text has been examined, the program outputs the list of misspelled words.

An improvement would be to suggest alternative words for each misspelling. There are various heuristics that can be used to discover alternatives. The technique we use here is to simply exchange adjacent letters. To find these, a call to the following function is inserted into the loop that displays the misspellings:

Previous fileTop of DocumentContentsIndex pageNext file