ctype.h

This page was translated by a robot.

When programming with C or C++, character strings are often used, which represent a sequence of individual characters. The individual characters are usually charhandled with the type and interpreted as ASCII characters . The standard library ctypehelps to work with these characters, noting that ONLY single 7-bit ASCII characters are explicitly supported with this library.

C
C++
#include <ctype.h>
#include <cctype>

Identifying ASCII Types

The following functions return either 0or 1depending on whether the passed ASCII character matches the criteria.

int islower (int asc);	   lowercase letters
int isupper (int asc);	   capital letter
int isalpha (int asc);	   Letters
int isdigit (int asc);	   digits
int isxdigit(int asc);	   Hexadecimal digits
int isalnum (int asc);	   letters and digits
int ispunct (int asc);	   punctuation marks
int isgraph (int asc);	   Graphic signs
int isblank (int asc);	   tab and space
int isspace (int asc);	   whitespaces
int isprint (int asc);	   Printable characters
int iscntrl (int asc);	   control characters
int isascii (int asc);	   ASCII characters

To illustrate which values ​​are returned by which function, the following is a list in tabular form. The various ASCII characters are shown horizontally, sorted by coding.

          0x00        0x0a  0x0e       ! 0 : A G [ a g {
            -    tab    -     -  space - - - - - - - - -  0x7f 
          0x08        0x0d  0x1f       / 9 @ F Z ` f z ~
---------------------------------------------------------------
islower     0     0     0     0    0   0 0 0 0 0 0 1 1 0    0
isupper     0     0     0     0    0   0 0 0 1 1 0 0 0 0    0
isalpha     0     0     0     0    0   0 0 0 1 1 0 1 1 0    0
isdigit     0     0     0     0    0   0 1 0 0 0 0 0 0 0    0
isxdigit    0     0     0     0    0   0 1 0 1 0 0 1 0 0    0
isalnum     0     0     0     0    0   0 1 0 1 1 0 1 1 0    0
ispunct     0     0     0     0    0   1 0 1 0 0 1 0 0 1    0
isgraph     0     0     0     0    0   1 1 1 1 1 1 1 1 1    0
isblank     0     1     0     0    1   0 0 0 0 0 0 0 0 0    0
isspace     0     1     1     0    1   0 0 0 0 0 0 0 0 0    0
isprint     0     0     0     0    1   1 1 1 1 1 1 1 1 1    0
iscntrl     1     1     1     1    0   0 0 0 0 0 0 0 0 0    1
isascii     1     1     1     1    1   1 1 1 1 1 1 1 1 1    1

It should be noted that the ASCII character set is only 0 - 127defined for values ​​of . Thus, any number not in this range is not considered an ASCII character, and all functions return in this case 0.

The following functions can also be found in the library header, but these functions (from the author's point of view) seem to have lost their original meaning and are no longer useful today:

int isnumber (int asc);      Same as isdigit
int ishexnumber(int asc);    Same as isxdigit
int isrune (int asc);        Same as isascii
int isideogram (int asc);    always 0
int isphonogram(int asc);    always 0
int isspecial (int asc);     always 0

Conversion of Characters

In addition to identification functions, the ASCII library also has functions that convert characters:

int tolower (int asc);      Conversion to lowercase
int toupper (int asc);      Conversion to uppercase
int toascii (int asc);      Conversion to ASCII characters
int digittoint(int asc);    Converting a hexadecimal digit to a int.

The functions tolowerand toupperonly convert letters explicitly. All other characters are returned without modification. The function toasciisimply deletes all bits that do not belong to the lowest 7 bits of the ASCII standard. The function digittointconverts a single hexadecimal digit to its numeric representation.