Skip to Main Content
Mastering Algorithms with C
book

Mastering Algorithms with C

by Kyle Loudon
August 1999
Intermediate to advanced content levelIntermediate to advanced
560 pages
18h 57m
English
O'Reilly Media, Inc.
Content preview from Mastering Algorithms with C

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. * * * *****************************************************************************/ ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Introducing Algorithms in C: A Step by Step Guide to Algorithms in C

Introducing Algorithms in C: A Step by Step Guide to Algorithms in C

Luciano Manelli
Head First C

Head First C

David Griffiths, Dawn Griffiths

Publisher Resources

ISBN: 1565924533Supplemental ContentErrata Page