212 Chapter 10 Character Strings
Program 10.7 Output
Well, here goes. - words = 3
And here we go... again. - words = 5
The alphabeticfunction is straightforward enough—it simply tests the value of the
character passed to it to determine if it is either a lowercase or uppercase letter. If it is
either, the function returns true, indicating that the character is alphabetic; otherwise,
the function returns false.
The countWordsfunction is not as straightforward.The integer variable iis used as
an index number to sequence through each character in the string.The integer variable
lookingForWordis used as a flag to indicate whether you are currently in the process of
looking for the start of a new word. At the beginning of the execution of the function,
you obviously arelooking for the start of a new word, so this flag is set to true.The
local variable wordCountis used for the obvious purpose of counting the number of
words in the character string.
For each character inside the character string, a call to the alphabeticfunction is
made to determine whether the character is alphabetic. If the character is alphabetic, the
lookingForWordflag is tested to determine if you are in the process of looking for a
new word. If you are, the value of wordCountis incremented by 1, and the
lookingForWordflag is set to false, indicating that you are no longer looking for the
start of a new word.
If the character is alphabetic and the lookingForWordflag is false, this means that
you are currently scanning insidea word. In such a case, the forloop is continued with
the next character in the string.
If the character is not alphabetic—meaning either that you have reached the end of a
word or that you have still not found the beginning of the next word—the flag
lookingForWordis set to true(even though it might already be true).
When all of the characters inside the character string have been examined, the func-
tion returns the value of wordCountto indicate the number of words that were found in
the character string.
It is helpful to present a table of the values of the various variables in the countWords
function to see how the algorithm works.Table 10.1 shows such a table, with the first
call to the countWordsfunction from the preceding program as an example.The first
line of Table 10.1 shows the initial value of the variables wordCountand
lookingForWordbefore the forloop is entered. Subsequent lines depict the values of
the indicated variables each time through the forloop. So, the second line of the table
shows that the value of wordCounthas been set to 1 and the lookingForWordflag set to
false( 0 ) after the first time through the loop (after the 'W'has been processed).The
last line of the table shows the final values of the variables when the end of the string is
reached.You should spend some time studying this table, verifying the values of the indi-
cated variables against the logic of the countWordsfunction. After this has been accom-
plished, you should then feel comfortable with the algorithm that is used by the function
to count the number of words in a string.