Using spell checkers has become an expected part of preparing all types of documents. From a computing standpoint, a basic spell checker works simply by checking words in a string of text against a dictionary. The dictionary contains the set of acceptable words.
The example presented here consists of a function,
spell (see Examples Example
12.9 and Example 12.10), that
checks the spelling of words from a string of text one word at a time.
The function accepts three arguments:
dictionary
is a sorted array of acceptable
strings, size
is the number of strings in
the dictionary, and word
is the word to
check. The function calls bisearch to look
up word
in
dictionary
. If it finds the word, it is
spelled correctly.
The runtime complexity of spell is
O (lg n), the same time as
bisearch, where n is the
number of words in dictionary
. The runtime
complexity of checking an entire document is O
(m lg n), where
m is the number of words in the text to validate
and n is the number of words in the
dictionary.
/***************************************************************************** * * * -------------------------------- spell.h ------------------------------- * * * *****************************************************************************/ #ifndef SPELL_H #define SPELL_H /***************************************************************************** * * * Define the maximum size for words in the dictionary. * * * *****************************************************************************/ #define SPELL_SIZE 31 /***************************************************************************** * * * --------------------------- Public Interface --------------------------- * * * *****************************************************************************/ int spell(char (*dictionary)[SPELL_SIZE], int size, const char *word); #endif
/***************************************************************************** * * * -------------------------------- spell.c ------------------------------- * * * *****************************************************************************/ #include <string.h> #include "search.h" #include "spell.h" /***************************************************************************** * * * ------------------------------ compare_str ----------------------------- * * * *****************************************************************************/ static int compare_str(const void *str1, const void *str2) { int retval; if ((retval = strcmp((const char *)str1, (const char *)str2)) > 0) return 1; else if (retval < 0) return -1; else return 0; } /***************************************************************************** * * * --------------------------------- spell -------------------------------- * * * *****************************************************************************/ int spell(char (*dictionary)[SPELL_SIZE], int size, const char *word) { /***************************************************************************** * * * Look up the word. * * * *****************************************************************************/ if (bisearch(dictionary, word, size, SPELL_SIZE, compare_str) >= 0) return 1; else return 0; }