A number of functions for classifying and changing the case of characters with type char are defined in the header file ctype.h. These functions, whose names begin with is... or to..., accept a one-character argument whose value is between 0 and 255, or EOF.
The is... functions, listed in Table 1-39, test whether the character is a member of a specific category of characters. They return "true," i.e., a non-zero value, if the character is in the given category. If not, the return value is 0, or "false."
The following example reads a character and then tests to see whether it is a digit:
int c = getchar(); // Read a character
if ( isdigit( c ) ) ...// Is it a decimal digit?
The to... functions are used to convert characters from upper- to lower-case and vice versa, as shown in Table 1-40.
Table 1-40. Case mapping functions |
|
Conversion |
Function |
Upper- to lower-case |
int tolower( int c ); |
Lower- to upper-case |
int toupper( int c ); |
The corresponding functions for wide characters, with type wchar_t, are declared in the header file wctype.h(*). Their names are similar to those in Table 1-39 and Table 1-40, but start with isw... and tow.... These functions expect one character argument of type wint_t whose value is between 0 and 32767, or WEOF.
For wide characters there are also the extensible classification and mapping functions, iswctype() and towctrans(). These functions provide flexible, locale-specific testing and mapping of wide characters. Before one of these functions is used, the desired test criterion or mapping information must be registered by a call to wctype() or wtrans():
iswctype( wc, wctype( "lower" ));
towctrans( wc, wctrans( "upper" ));
These calls are equivalent to iswlower(wc); and towupper(wc);. The function wctype() returns a value of type wctype_t, and wctrans() has a return value of type wctrans_t.
Single-byte characters of type unsigned char can be converted to the type wchar_t using the function btowc(), which is declared in wchar.h(*). The opposite conversion is performed by the function wctob(). If the character cannot be converted, these functions return EOF or WEOF.
All of these functions take language-specific particularities of the current locale into account (see the later section Section 1.26).