Example program: - a spelling checker – HP Integrity NonStop H-Series User Manual
Page 91
![background image](/manuals/396950/91/background.png)
Click on the banner to return to the user guide home page.
©Copyright 1996 Rogue Wave Software
Example Program: - A Spelling Checker
Obtaining the Sample Program
A simple example program that uses a set is a spelling checker. The checker takes as arguments
two input streams; the first representing a stream of correctly spelled words (that is, a
dictionary), and the second a text file. First, the dictionary is read into a set. This is performed
using a copy() 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, then they are added to a set of misspelled words. After the entire text
has been examined, the program outputs the list of misspelled words.
void spellCheck (istream & dictionary, istream & text)
{
typedef set
stringset words, misspellings;
string word;
istream_iterator
// first read the dictionary
copy (dstream, eof, inserter(words, words.begin()));
// next read the text
while (text >> word)
if (! words.count(word))
misspellings.insert(word);
// finally, output all misspellings
cout << "Misspelled words:" << endl;
copy (misspellings.begin(), misspellings.end(),
ostream_iterator
}
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 will use here is to simply
exchange adjacent letters. To find these, a call on the following function is inserted into the loop
that displays the misspellings.