Skip to content
Tutorials Center
My learning

Topics

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.

C++
#include <cctype>

unsigned char c = 'x';
bool letter = std::isalpha(c);   // true
char up = std::toupper(c);       // 'X'

Try it

C++ Classifying every character of a string

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.

C++ Changing case and parsing with a safe wrapper

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

C++
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 char when the argument comes from a char: std::isalpha(static_cast<unsigned char>(ch)). In lambdas, declare the parameter as unsigned char.
  • The result is an int. Compare with != 0 or convert to bool; never compare to 1 or true.
  • They depend on the C locale. In the default "C" locale only ASCII letters count as alphabetic. After std::setlocale the result for bytes above 127 can change. The overloads in <locale> such as std::isalpha(ch, loc) take a locale explicitly and a char parameter, 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++.
  • isdigit accepts only '0' to '9'. It is the right tool for ASCII digit checks; isxdigit adds hexadecimal letters. Unlike isalnum, none of them accept signs, points or exponents. For number parsing use to_chars and from_chars.
  • toupper and tolower change one character and have no knowledge of special cases (the German sharp s has no one-character uppercase form). They return int, so assign to a char or cast.
  • Speed. They are table lookups and are typically inlined; a loop with std::transform is fast. For ASCII-only code, a hand-written range test (c >= 'a' && c <= 'z') is equally correct and independent of the locale.