Previous: 8.1 The C Standard Library
Up: 8.1 The C Standard Library
Next: 8.1.2 Math Routines
Previous Page: 8.1 The C Standard Library
Returns:
Description: These are macros that classify a character, c, given as an integer type (ASCII) value. They return non-zero if TRUE and zero if FALSE.
Returns: converted value of c.
Description: Converts an integer, c, to ASCII format by clearing all but the lower seven bits. The value returned is in the range 0-127.
Returns: Lower case value of c if c was upper case, c otherwise.
Returns: Converts c to upper case if c is lower case; otherwise, c is left unchanged.
Note that all the above library character routines use an int type argument. Since the value of a character is its ASCII value of type int, passing a char type argument to these routines is the same as passing an int type ASCII value.
Let us use some of the above library routines to write a variation on our previous program to pick out words in the input text. The revised program only picks out valid words; namely identifiers. We will assume that a valid identifier starts with a letter and may be followed by any number of letters and/or digits. White space delimits an identifier; otherwise, it is ignored. Any character that does not belong in an identifier is an illegal character; and also delimits an identifier.
We will need to test each character to see if it is a letter, a digit, a white space, etc. We will use library functions isalpha(), isalnum(), and isspace() to test for these characters. The descriptions for them states that we must include file < ctype.h>. In addition to finding and printing identifiers, the program also keeps a count of them.
The only change in the previous algorithm is that now we start a word if and only if it starts with a letter. Once a word is started, it continues as long as characters are letters or digits; otherwise, the word is terminated and counted. The program is shown in Figure 8.1.
We test if the first character after white space is a letter. If so, we build an identifier. Otherwise, if it is EOF, we terminate the loop. Otherwise, it must be an illegal character.
Sample Session: