Character Handling Library
The character-handling library (<ctype.h>) includes several functions that perform useful tests and manipulations of character data. Each function receives a character represented as an int or EOF as an argument.
Following table summarizes the functions of the character-handling library.
Prototype |
Function Description |
---|---|
int isdigit( int c ); |
Returns a true value if c is a digit and 0 (false) otherwise. |
int isalpha( int c ); |
Returns a true value if c is a letter and 0 otherwise. |
int isalnum( int c ); |
Returns a true value if c is a digit or a letter and 0 otherwise. |
int isxdigit( int c ); |
Returns a true value if c is a hexadecimal digit character and 0 otherwise.
|
int islower( int c ); |
Returns a true value if c is a lowercase letter and 0 otherwise. |
int isupper( int c ); |
Returns a true value if c is an uppercase letter and 0 otherwise. |
int tolower( int c ); |
If c is an uppercase letter, tolower returns c as a lowercase letter. Otherwise, tolower returns the argument unchanged. |
int toupper( int c ); |
If c is a lowercase letter, toupper returns c as an uppercase letter. Otherwise, toupper returns the argument unchanged. |
int isspace( int c ); |
Returns a true value if c is a white-space character newline ('\n'), space (' '), form feed ('\f'), carriage return ('\r'), horizontal tab ('\t') or vertical tab ('\v')—and 0 otherwise. |
int iscntrl( int c ); |
Returns a true value if c is a control character and 0 otherwise. |
int ispunct( int c ); |
Returns a true value if c is a printing character other than space, a digit, or a letter and returns 0 otherwise. |
int isprint( int c ); |
Returns a true value if c is a printing character including a space (' ') and returns 0 otherwise. |
int isgraph( int c ); |
Returns a true value if c is a printing character other than space (' ') and returns 0 otherwise. |
Functions isdigit, isalpha, isalnum and isxdigit
Following Example demonstrates functions isdigit, isalpha, isalnum and isxdigit. Function isdigit determines whether its argument is a digit (0–9).
Function isalpha determines whether its argument is an uppercase (A–Z) or lowercase letter (a–z). Function isalnum determines whether its argument is an uppercase letter, a lowercase letter or a digit. Function isxdigit determines whether its argument is a hexadecimal digit (A–F, a–f, 0–9).
Example:
/*Using functions isdigit, isalpha, isalnum, and isxdigit */
#include <stdio.h>
#include <ctype.h>
int main( void )
{
printf( "%s\n%s%s\n%s%s\n\n", "According to isdigit: ",
isdigit( ‘8’) ? “8 is a ” : “8 is not a ” , “digit”,
isdigit( ‘ # ’) ? “# is a ” : “# is not a ” , “digit” );
.
printf( "%s\n%s%s\n%s%s\n%s%s\n%s%s\n\n",
"According to isalpha:",
isalpha( ‘A’) ? “A is a ” : “A is not a ” , “letter”,
isalpha( ‘b’) ? “b is a ” : “b is not a ” , “letter”,
isalpha( ‘&’) ? “& is a ” : “& is not a ” , “letter”,
isalpha( ‘4’) ? “4 is a ” : “4 is not a ” , “letter” );
printf( "%s\n%s%s\n%s%s\n%s%s\n\n",
"According to isalnum:",
isalnum( ‘A’) ? “A is a ” : “A is not a ” , “digit or a letter”,
isalnum( ‘&’) ? “& is a ” : “& is not a ” , “digit or a letter”,
isalnum( ‘#’) ? “# is a ” : “# is not a ” , “digit or a letter”);
printf( "%s\n%s%s\n%s%s\n%s%s\n%s%s\n%s%s\n",
"According to isxdigit:",
isxdigit( ‘F’) ? “F is a ” : “F is not a ” , “hexadecimal digit “,
isxdigit( ‘J’) ? “J is a ” : “J is not a ” , “hexadecimal digit “,
isxdigit( ‘7’) ? “7 is a ” : “7 is not a ” , “hexadecimal digit “,
isxdigit( ‘$’) ? “$ is a ” : “$ is not a ” , “hexadecimal digit “,
isxdigit( ‘f’) ? “f is a ” : “f is not a ” , “hexadecimal digit “ );
return 0;
}
Output:
According to isdigit:
8 is a digit
# is not a digit
According to isalpha:
A is a letter
b is a letter
& is not a letter
4 is not a letter
According to isalnum:
A is a digit or a letter
8 is a digit or a letter
# is not a digit or a letter
According to isxdigit:
F is a hexadecimal digit
J is not a hexadecimal digit
7 is a hexadecimal digit
$ is not a hexadecimal digit
f is a hexadecimal digit
The program uses the conditional operator (?:) to determine whether the string " is a " or the string " is not a " should be printed in the output for each character tested.
For example, the expression
isdigit( '8' ) ? "8 is a " : "8 is not a "
indicates that if '8' is a digit, the string "8 is a " is printed, and if '8' is not a digit (i.e., isdigit returns 0), the string "8 is not a " is printed.
Functions islower, isupper, tolower and toupper
The example demonstrates functions islower, isupper, tolower and toupper. Function islower determines whether its argument is a lowercase letter (a–z). Function isupper determines whether its argument is an uppercase letter (A–Z). Function tolower converts an uppercase letter to a lowercase letter and returns the lowercase letter.
If the argument is not an uppercase letter, tolower returns the argument unchanged. Function toupper converts a lowercase letter to an uppercase letter and returns the uppercase letter. If the argument is not a lowercase letter, toupper returns the argument unchanged.
Example:
/*Using functions islower, isupper, tolower, toupper */
#include <stdio.h>
#include <ctype.h>
int main( void )
{
printf( "%s\n%s%s\n%s%s\n%s%s\n%s%s\n\n",
"According to islower:",
islower( ‘p’ ) ? “p is a” : “p is not a”, “lowercase letter”,
islower( ‘p’ ) ? “p is a” : “p is not a”, “lowercase letter”,
islower( ‘5’ ) ? “5 is a” : “5 is not a”, “lowercase letter”,
islower( ‘!’ ) ? “! is a” : “! is not a”, “lowercase letter”,
.
printf( "%s\n%s%s\n%s%s\n%s%s\n%s%s\n\n",
"according to isupper:",
isupper(‘d’) ? “d is an ” : “d is not an”, “uppercase letter”,
isupper(‘d’) ? “d is an ” : “d is not an”, “uppercase letter”,
isupper(‘8’) ? “8 is an ” : “8 is not an”, “uppercase letter”,
isupper(‘$’) ? “$ is an ” : “$ is not an”, “uppercase letter” );
printf( "%s%c\n%s%c\n%s%c\n%s%c\n",
"u converted to uppercase is ", toupper( ‘u’ ),
"7 converted to uppercase is ", toupper( ‘7’ ),
"$ converted to uppercase is ", toupper( ‘$’ ),
"l converted to uppercase is ", toupper( ‘l’ ),
return 0;
}
Output:
According to islower:
p is a lowercase letter
P is not a lowercase letter
5 is not a lowercase letter
! is not a lowercase letter
According to isupper:
D is an uppercase letter
d is not an uppercase letter
8 is not an uppercase letter
$ is not an uppercase letter
u converted to uppercase is U
7 converted to uppercase is 7
$ converted to uppercase is $
L converted to lowercase is l