Binary Search Example: Spell Checking

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.

Example 12.9. Header for Spell Checking
/***************************************************************************** * * * -------------------------------- spell.h ------------------------------- * * * *****************************************************************************/ #ifndef SPELL_H #define SPELL_H /***************************************************************************** * * * Define the maximum size for words in the dictionary. * * * *****************************************************************************/ ...

Get Mastering Algorithms with C now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.