Skip to content
DryPerspective edited this page Nov 20, 2023 · 1 revision

The standard library cctype header's functions have two primary issues:

  • It is undefined behaviour to take their address, meaning they cannot be used directly in range functions and algorithm calls.

  • They are undefined behaviour to use if the system's char type cannot be represented as either unsigned char or EOF.

This header contains functions which address both of these. As user-defined functions, their address can be freely taken. Similarly they will cause a compilation failure if their use is attempted on a system where a char cannot be safely cast to unsigned char in a value-preserving way.

List of Features

isalnum Returns whether a given character is alphanumeric
isalpha Returns whether a given character is alphabetic
islower Returns whether a given character is lower case
isupper Returns whether a given character is upper case
isdigit Returns whether a given character is a numeric digit
isxdigit Returns whether a given character is a hex digit (0123456789ABCDEFabcdef)
iscntrl Returns whether a given character is a control character
isgraph Returns whether a given character is a graphical character
isspace Returns whether a given character is a space character
isprint Returns whether a given character is a printable character
ispunct Returns whether a given character is a punctuation character
tolower Converts a given character to lower case
toupper Converts a given character to upper case

Sample code

#include <algorithm>
#include <string>

#include "cpp98/cctype.h"

int main(){
    std::string mix("a MiX Of CaSeS");
    int num_upper_case = std::count_if(mix.begin(), mix.end(), dp::isupper); //Would be UB to use std::isupper
    //Convert all to lower
    std::transform(mix.begin(), mix.end(), mix.begin(), dp::tolower); //Would also be UB to use std::tolower
}

Clone this wiki locally