08.01.2023 Views

Learn to Program with C_ Learn to Program using the Popular C Programming Language ( PDFDrive )

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

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

Chapter 9 ■ Searching, Sorting, and Merging

those 1

thoughts 1

treat 1

triumph 1

two 1

unforgivin 1

with 2

worth 1

you 4

your 2

yours 1

Comments on Program P9.4

• For our purposes, we assume that a word begins with a letter and consists

of letters only. If you want to include other characters (such as a hyphen or

apostrophe), you need change only the getWord function.

• MaxWords denotes the maximum number of distinct words catered for. For

testing the program, we have used 50 for this value. If the number of distinct

words in the passage exceeds MaxWords (50, say), any words after the 50th

will be read but not stored, and a message to that effect will be printed.

However, the count for a word already stored will be incremented if it is

encountered again.

• MaxLength (we use 10 for testing) denotes the maximum length of a word.

Strings are declared using MaxLength+1 (defined as MaxWordBuffer) to cater

for \0, which must be added at the end of each string.

• main checks that the input file exists and that the output file can be created.

Next, it initializes the frequency counts to 0. It then processes the words in

the passage based on the outline shown at the beginning of this Section.

• getWord reads the input file and stores the next word found in its string

argument. It returns 1 if a word is found and 0, otherwise. If a word is longer

than MaxLength, only the first MaxLength letters are stored; the rest are read

and discarded. For example, unforgiving is truncated to unforgivin using

a word size of 10.

• All words are converted to lowercase so that, for instance, The and the are

counted as the same word.

• We wrote binarySearch so that if the word is found, its location (loc, say) is

returned. If not found, the location in which the word should be inserted is

returned. The test

• if (strcmp(word, wordList[loc]) == 0)

270

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!