20.09.2015 Views

Programming in C

Kochan - ProgramminginC

Kochan - ProgramminginC

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

222 Chapter 10 Character Str<strong>in</strong>gs<br />

Program 10.9<br />

Cont<strong>in</strong>ued<br />

else<br />

pr<strong>in</strong>tf ("Sorry, the word %s is not <strong>in</strong> my dictionary.\n", word);<br />

}<br />

return 0;<br />

Program 10.9<br />

Output<br />

Enter word: agar<br />

a jelly made from seaweed<br />

Program 10.9<br />

Output (Rerun)<br />

Enter word: accede<br />

Sorry, the word accede is not <strong>in</strong> my dictionary.<br />

The lookup function sequences through each entry <strong>in</strong> the dictionary. For each such<br />

entry, the function calls the equalStr<strong>in</strong>gs function to determ<strong>in</strong>e if the character str<strong>in</strong>g<br />

search matches the word member of the particular dictionary entry. If it does match, the<br />

function returns the value of the variable i, which is the entry number of the word that<br />

was found <strong>in</strong> the dictionary.The function is exited immediately upon execution of the<br />

return statement, despite the fact that the function is <strong>in</strong> the middle of execut<strong>in</strong>g a for<br />

loop.<br />

If the lookup function exhausts all the entries <strong>in</strong> the dictionary without f<strong>in</strong>d<strong>in</strong>g a<br />

match, the return statement after the for loop is executed to return the “not found”<br />

<strong>in</strong>dication (–1) back to the caller.<br />

A Better Search Method<br />

The method used by the lookup function to search for a particular word <strong>in</strong> the dictionary<br />

is straightforward enough; the function simply performs a sequential search through<br />

all the entries <strong>in</strong> the dictionary until either a match is made or the end of the dictionary<br />

is reached. For a small-sized dictionary like the one <strong>in</strong> your program, this approach is<br />

perfectly f<strong>in</strong>e. However, if you start deal<strong>in</strong>g with large dictionaries conta<strong>in</strong><strong>in</strong>g hundreds<br />

or perhaps even thousands of entries, this approach might no longer be sufficient because<br />

of the time it takes to sequentially search through all of the entries.The time required<br />

can be considerable—even though considerable <strong>in</strong> this case could mean only a fraction<br />

of a second. One of the prime considerations that must be given to any sort of <strong>in</strong>formation<br />

retrieval program is that of speed. Because the search<strong>in</strong>g process is one that is so<br />

frequently used <strong>in</strong> computer applications, much attention has been given by computer<br />

scientists to develop<strong>in</strong>g efficient algorithms for search<strong>in</strong>g (about as much attention as has<br />

been given to the process of sort<strong>in</strong>g).

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!