DekGenius.com
Team LiB   Previous Section   Next Section

13.4 <cctype>

The <cctype> header (from the C standard <ctype.h> header) declares a number of functions for testing and mapping narrow character types. For working with wide characters, see <cwctype>.

All the functions take int parameters, but the value of the parameter must be an unsigned char. Most programs work with ordinary char, so you must cast the parameters and some of the return types:

char c;
if (std::isalnum(static_cast<unsigned char>(c)))
...
c = static_cast<char>(std::tolower(static_cast<unsigned char>(c)));

The only other value that is permitted is EOF.

figs/acorn.gif

These functions get their information from the current locale, as set by calling setlocale. The "C" locale is the only one with behavior that is defined in the standard; all other locales can define these functions to include or exclude different characters. Even in the "C" locale, the behavior of some functions depends on the execution character set (see Chapter 1). One requirement for all locales is that isalpha, iscntrl, isdigit, ispunct, and the space character (' ') are mutually exclusive. See <clocale> for information about the setlocale function.

See the <locale> header for a more flexible (albeit more complicated) mechanism for testing and transforming characters. Each of the functions in this section has a corresponding function in <locale> that takes an explicit locale argument. Also, the ctype facet supports similar functionality.

isalnum function Tests for an alphanumeric character

int isalnum(int c)

The isalnum function returns true (nonzero) if c is alphanumeric—that is, it returns isalpha(c) || isnumeric(c).

isalpha function Tests for an alphabetic character

int isalpha(int c)

The isalpha function returns true (nonzero) if c is alphabetic. In the "C" locale, this includes only the characters for which islower(c) or isupper(c) is true. For other locales, other characters might be alphabetic.

iscntrl function Tests for a control character

int iscntrl(int c)

The iscntrl function returns true (nonzero) if c is a control character. The set of control characters depends on the locale and character set. In the 7-bit ASCII character set, the control characters are '\0'-'\x1F' and '\x7F'; other implementations might have different control characters. By definition, a control character is any character that is not a printable character (isprint).

isdigit function Tests for a digit character

int isdigit(int c)

The isdigit function returns true (nonzero) if c is a decimal digit character—that is, '0'-'9'—regardless of locale.

isgraph function Tests for a graphic character

int isgraph(int c)

The isgraph function returns true (nonzero) if c is any printing character (isprint) except space (' '). The set of printing characters varies with locale and character set.

islower function Tests for a lowercase letter

int islower(int c)

The islower function returns true (nonzero) if c is a lowercase letter. In the "C" locale, only the characters 'a'-'z' are lowercase; different locales can define other lowercase characters.

isprint function Tests for a printable character

int isprint(int c)

The isprint function returns true (nonzero) if c is a printing character, including space (' '), according to the locale and character set. Informally, a printing character occupies space on a display device.

ispunct function Tests for a punctuation character

int ispunct(int c)

The ispunct function returns true (nonzero) for a punctuation character, that is, any printable character (isprint) other than space (' ') and alphanumeric characters (isalnum).

isspace function Tests for a white space character

int isspace(int c)

The isspace function returns true (nonzero) if c is a whitespace character. In the "C" locale, the space (' '), form feed ('\f'), newline ('\n'), carriage return ('\r'), horizontal tab ('\t'), and vertical tab ('\v') characters are whitespace, but backspace ('\b') is not. Different locales can define other whitespace characters.

isupper function Tests for an uppercase letter

int isupper(int c)

The isupper function returns true (nonzero) if c is an uppercase letter. In the "C" locale, only the characters 'A'-'Z' are uppercase; different locales can define other uppercase characters

isxdigit function Tests for a hexadecimal digit character

int isxdigit(int c)

The isxdigit function returns true (nonzero) if c is any hexadecimal digit character—that is, '0'-'9', 'a'-'f', or 'A'-'F'—regardless of locale.

tolower function Converts a character to lowercase

int tolower(int c)

The tolower function converts uppercase characters to lowercase. If c is uppercase (that is, isupper(c) returns true), tolower returns the corresponding lowercase character (for which islower returns true) in the current locale, if there is such a character. Otherwise, it returns c.

toupper function Converts a character to uppercase

int toupper(int c)

The touoper function converts lowercase characters to uppercase. If c is lowercase (that is, islower(c) returns true), toupper returns the corresponding uppercase character (for which isupper returns true) in the current locale, if there is such a character. Otherwise, it returns c.

    Team LiB   Previous Section   Next Section