C++ Reference ยท Strings and I/O
C++ <cctype> character functions
Reference for C++ <cctype>: isalpha, isdigit, isspace, ispunct, toupper and tolower, why you must cast char to unsigned char, and locale and Unicode limits.
<cctype> provides small functions that classify and convert single characters: std::isalpha, std::isdigit, std::isalnum, std::isspace, std::isupper, std::islower, std::ispunct, std::isxdigit, std::isprint, std::iscntrl, std::isgraph, std::isblank, plus std::toupper and std::tolower. They come from C and are used all over C++ code to validate input, tokenize text and normalize case.
Each function takes an int and returns an int. The classification functions return non-zero for true and zero for false (not necessarily 1), so convert to bool when you store or print the result. The int parameter has a strict rule: the value must be representable as unsigned char or be EOF. Passing a negative char is undefined behavior.
#include <cctype> unsigned char c = 'x'; bool letter = std::isalpha(c); // true char up = std::toupper(c); // 'X'
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.
Syntax
int std::isalpha (int ch); // letter int std::isdigit (int ch); // 0-9 only int std::isalnum (int ch); // letter or digit int std::isspace (int ch); // space, \t \n \v \f \r int std::isblank (int ch); // space or \t int std::isupper (int ch); int std::islower(int ch); int std::ispunct (int ch); // printable, not alnum, not space int std::isxdigit(int ch); // 0-9 a-f A-F int std::isprint (int ch); int std::isgraph(int ch); int std::iscntrl(int ch); int std::toupper (int ch); int std::tolower(int ch);
| Name | Description |
|---|---|
ch |
A value in the range of unsigned char or EOF. Anything else is undefined behavior. |
Returns: the classification functions return non-zero if the character belongs to the class, otherwise 0. toupper and tolower return the converted character as an int (the character unchanged if there is no mapping).
Undefined behavior: calling with a negative value other than EOF, which happens when a plain char holding a byte above 127 (for example from UTF-8 text) is promoted to a negative int.
Usage notes
- Always cast through
unsigned charwhen the argument comes from achar:std::isalpha(static_cast<unsigned char>(ch)). In lambdas, declare the parameter asunsigned char. - The result is an
int. Compare with!= 0or convert tobool; never compare to1ortrue. - They depend on the C locale. In the default
"C"locale only ASCII letters count as alphabetic. Afterstd::setlocalethe result for bytes above 127 can change. The overloads in<locale>such asstd::isalpha(ch, loc)take a locale explicitly and acharparameter, so they have no cast problem. - No Unicode. These functions look at one byte. A UTF-8 character such as the letter รฉ is two bytes and is not a letter to
isalpha. See Unicode in C++. isdigitaccepts only'0'to'9'. It is the right tool for ASCII digit checks;isxdigitadds hexadecimal letters. Unlikeisalnum, none of them accept signs, points or exponents. For number parsing useto_charsandfrom_chars.toupperandtolowerchange one character and have no knowledge of special cases (the German sharp s has no one-character uppercase form). They returnint, so assign to acharor cast.- Speed. They are table lookups and are typically inlined; a loop with
std::transformis fast. For ASCII-only code, a hand-written range test (c >= 'a' && c <= 'z') is equally correct and independent of the locale.