Programming in C

(Barry) #1

222 Chapter 10 Character Strings


else
printf ("Sorry, the word %s is not in my dictionary.\n", word);

return 0;
}

Program 10.9 Output
Enter word: agar
a jelly made from seaweed

Program 10.9 Output (Rerun)
Enter word: accede
Sorry, the word accede is not in my dictionary.

The lookupfunction sequences through each entry in the dictionary. For each such
entry, the function calls the equalStringsfunction to determine if the character string
searchmatches the wordmember of the particular dictionary entry. If it does match, the
function returns the value of the variable i, which is the entry number of the word that
was found in the dictionary.The function is exited immediately upon execution of the
returnstatement, despite the fact that the function is in the middle of executing a for
loop.
If the lookupfunction exhausts all the entries in the dictionary without finding a
match, the returnstatement after the forloop is executed to return the “not found”
indication (–1) back to the caller.

A Better Search Method


The method used by the lookupfunction to search for a particular word in the diction-
ary is straightforward enough; the function simply performs a sequential search through
all the entries in the dictionary until either a match is made or the end of the dictionary
is reached. For a small-sized dictionary like the one in your program, this approach is
perfectly fine. However, if you start dealing with large dictionaries containing hundreds
or perhaps even thousands of entries, this approach might no longer be sufficient because
of the time it takes to sequentially search through all of the entries.The time required
can be considerable—even though considerable in this case could mean only a fraction
of a second. One of the prime considerations that must be given to any sort of informa-
tion retrieval program is that of speed. Because the searching process is one that is so
frequently used in computer applications, much attention has been given by computer
scientists to developing efficient algorithms for searching (about as much attention as has
been given to the process of sorting).

Program 10.9 Continued
Free download pdf