Most data is entered into computers as characters—including letters, digits and various special symbols. In this section, we discuss C++’s capabilities for examining and manipulating individual characters. In the remainder of the chapter, we continue the discussion of character-string manipulation that we began in Chapter 8.
The character-handling library includes several functions that perform useful tests and manipulations of character data. Each function receives a character—represented as an int—or EOF as an argument. Characters are often manipulated as integers. Remember that EOF normally has the value –1 and that some hardware architectures do not allow negative values to be stored in char variables. Therefore, the character-handling functions manipulate characters as integers. Figure 22.17 summarizes the functions of the character-handling library. When using functions from the character-handling library, include the <cctype> header.
Fig. 22.17 Character-handling library functions.
Prototype
Description
int isdigit(int c)
Returns 1 if c is a digit and 0 otherwise.
int isalpha(int c)
Returns 1 if c is a letter and 0 otherwise.
int isalnum(int c)
Returns 1 if c is a digit or a letter and 0 otherwise.
int isxdigit(int c)
Returns 1 if c is a hexadecimal digit character and 0 otherwise. (See Appendix D for a detailed explanation of binary, octal, decimal and hexadecimal numbers.)
int islower(int c)
Returns 1 if c is a lowercase letter and 0 otherwise.
int isupper(int c)
Returns 1 if c is an uppercase letter; 0 otherwise.
int tolower(int c)
If c is an uppercase letter, tolower returns c as a lowercase letter. Otherwise, tolower returns the argument unchanged.
int toupper(int c)
If c is a lowercase letter, toupper returns c as an uppercase letter. Otherwise, toupper returns the argument unchanged.
int isspace(int c)
Returns 1 if c is a whitespace character—newline ('
'), space (''), form feed ('f'), carriage return ('
'), horizontal tab (' '), or vertical tab ('v')—and 0 otherwise.
int iscntrl(int c)
Returns 1 if c is a control character, such as newline ('
'), form feed ('f'), carriage return ('
'), horizontal tab (' '), vertical tab ('v'), alert ('a'), or backspace ('')—and 0 otherwise.
int ispunct(int c)
Returns 1 if c is a printing character other than a space, a digit, or a letter and 0 otherwise.
int isprint(int c)
Returns 1 if c is a printing character including space ('') and 0 otherwise.
int isgraph(int c)
Returns 1 if c is a printing character other than space ('') and 0 otherwise.
Figure 22.18 demonstrates functions isdigit, isalpha, isalnum and isxdigit. Function isdigit determines whether its argument is a digit (0–9). Function isalpha determines whether its argument is an uppercase letter (A-Z) or a lowercase letter (a–z). Function isalnum determines whether its argument is an uppercase letter, a lowercase letter or a digit. Function isxdigit determines whether its argument is a hexadecimal digit (A– F, a–f, 0–9).
Figure 22.18 uses the conditional operator (?:) with each function to determine whether the string " is a " or the string " is not a " should be printed in the output for each character tested. For example, line 9 indicates that if '8' is a digit—i.e., if isdigit returns a true (nonzero) value—the string "8 is a " is printed. If '8' is not a digit (i.e., if isdigit returns 0), the string "8 is not a " is printed.
Figure 22.19 demonstrates functions islower, isupper, tolower and toupper. Function islower determines whether its argument is a lowercase letter (a–z). Function isupper determines whether its argument is an uppercase letter (A–Z). Function tolower converts an uppercase letter to lowercase and returns the lowercase letter—if the argument is not an uppercase letter, tolower returns the argument value unchanged. Function toupper converts a lowercase letter to uppercase and returns the uppercase letter—if the argument is not a lowercase letter, toupper returns the argument value unchanged.
Figure 22.20 demonstrates functions isspace, iscntrl, ispunct, isprint and isgraph. Function isspace determines whether its argument is a whitespace character, such as space (' '), form feed ('f'), newline ('
'), carriage return ('
'), horizontal tab (' ') or vertical tab ('v'). Function iscntrl determines whether its argument is a control character such as horizontal tab (' '), vertical tab ('v'), form feed ('f'), alert ('a'), backspace (''), carriage return ('
') or newline ('
'). Function ispunct determines whether its argument is a printing character other than a space, digit or letter, such as $, #, (,), [,], {, }, ;, : or %. Function isprint determines whether its argument is a character that can be displayed on the screen (including the space character). Function isgraph tests for the same characters as isprint, but the space character is not included.