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.
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
|
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
|
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
|
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
|
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
|
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
|
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
|
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
|
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
|
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
|
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
|
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
|
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
|
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.
|