Any C programmer eager to mess with characters or strings knows about the handy ctype functions. I use this name because these functions, which include a few macros, are defined in the ctype.h header file. Their job is to manipulate and examine characters.
I divide the ctype functions into the “to” and “is” categories.
The “to” functions manipulate characters. Only two of them are available: toupper() and tolower(), which convert a character from lowercase to uppercase and vice-versa, respectively. Both functions start with “to.”
The “is” functions return TRUE or FALSE based on the character’s attributes. For example, isalpha() returns TRUE when the character examined is alphabetic, upper- or lowercase. The function starts with “is,” which is how I define this category. Lotsa “is” ctype functions are available:
isalnum()
isalpha()
isascii()
isblank()
iscntrl()
isdigit()
isgraph()
islower()
isprint()
ispunct()
isspace()
isupper()
isxdigit())
All of these functions are defined in the ctype.h header file. They all have a similar man page format. For example:
int isspace(int c)
The argument c is specified as an integer, though it must have the value of an unsigned char or EOF. (The EOF is why the prototype is an integer, which allows this function to work with standard I/O.)
The return value is non-zero for a TRUE or positive result, zero otherwise. For example, isspace(' ') returns TRUE when character c is a whitespace character.
These functions work reliably on standard ASCII characters. Supposedly, they can also function in other languages when the locale is set, and variations on the functions are available to handle different alphabets. I’ve been unable to verify whether this feature works. So, my exploration of these functions is limited to standard ASCII, the Latin alphabet.
It’s easy to guess what each of the function does based on the name, though some are kinda weird. Here are brief descriptions:
isalnum() |
returns TRUE for letters of the alphabet (both upper- and lowercase) as well as digits 0 through 9. |
isalpha() |
returns TRUE for an alphabetic character, both upper- and lowercase. |
isascii() |
returns TRUE if character c is an ASCII character, codes 0 through 127. |
isblank() |
returns TRUE for a space (' ') or tab ('\t') character. |
iscntrl() |
returns TRUE for a control character, ASCII codes 0 through 31 (0x00 through 0x1F). |
isdigit() |
returns TRUE when the character is a digit, zero through 9. |
isgraph() |
returns TRUE for all printable characters except for a space. |
islower() |
returns TRUE for a lowercase character. |
isprint() |
returns TRUE for all printable characters, including the space. |
ispunct() |
returns TRUE for a character that is not a space or alphanumeric. |
isspace() |
returns TRUE for any whitespace character, including space, tab, form feed, newline, carriage return, and vertical tab. |
isupper() |
returns TRUE for an uppercase letter. |
isxdigit() |
returns TRUE for characters used in hexadecimal values, zero through 9 and A through F both upper- and lowercase. |
Over the next few weeks, I’ll cover these functions and how they work. I’ll also present code that emulates the functions just because it’s a fun thing to do!
These are “ctype” functions.