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

8.1.1 Character Processing Routines

Character Classification Routines

Character Conversion Routines

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.

Character Routines Programming Examples

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:



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

tep@wiliki.eng.hawaii.edu
Wed Aug 17 09:15:23 HST 1994