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. For example:
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. 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. One requirement is that isalpha
, iscntrl
, isdigit
, ispunct
and the space character (' '
) are mutually exclusive.
See the <locale>
header for more information about character types and locales.
int isalnum(int c)
The isalnum
function returns true if c
is alphanumeric, that is, it returns isalpha(c)
|| isnumeric(c)
.
int isalpha(int c)
The isalpha
function returns true 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.
int iscntrl(int c)
The iscntrl
function returns true if c
is a control character. In the 7-bit ASCII character set, the control characters are '\0'
to '\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
).
int isdigit(int c)
The isdigit
function returns true if c
is a decimal digit character, that is, '0'
through '9'
, regardless of locale.
int isgraph(int c)
The isgraph
function returns true if c
is any printing character (isprint
) except space (' '
). The set of printing characters varies with locale.
int islower(int c)
The islower
function returns true if c
is a lowercase letter. In the "C"
locale, the characters 'a'
through 'z'
are lowercase; different locales can define other lowercase characters.
int isprint(int c)
The isprint
function returns true if c
is a printing character, including space (' '
), according to the locale. Informally, a printing character is one that occupies space on a display device.
int ispunct(int c)
The ispunct
function returns true for a punctuation character, that is, any printable character (isprint
) other than space (' '
) and alphanumeric characters (isalnum
).
int isspace(int c)
The isspace
function returns true if c
is a white space character. In the "C"
locale, the space (' '
), form feed ('\f'
), newline ('\n'
), carriage return ('\r'
), horizontal tab ('\t'
), and vertical tab ('\v'
) characters are white space, but back space ('\b'
) is not. Different locales can define other white space characters.
int isupper(int c)
The isupper
function returns true if c
is an uppercase letter. In the "C"
locale, the characters 'A'
through 'A'
are uppercase; different locales can define other uppercase characters
int isxdigit(int c)
The isxdigit
function returns true if c
is any hexadecimal digit character, that is, '0'
through '9'
, 'a'
through 'f'
, or 'A'
though 'F'
, regardless of locale.
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), if there is such a character. Otherwise it returns c
.
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), if there is such a character. Otherwise it returns c
.