C ctype
Library
The <ctype.h>
header in C provides a variety of functions for classifying and modifying characters. These functions enable you to easily check character properties or convert characters between upper and lower case.
C ctype
Library
The <ctype.h>
header in C provides a range of functions for classifying and modifying characters. These functions allow you to easily check the characteristics of characters or convert them between upper and lower case.
Character Classification Functions
Below is a list of functions provided by the <ctype.h>
library that are used to classify characters:
Function | Description |
---|---|
isalnum() |
Checks whether a character is alphanumeric (either a letter or a digit). |
isalpha() |
Checks whether a character is a letter. |
isblank() |
Checks whether a character is a blank space or a tab. |
iscntrl() |
Checks whether a character is a control character. |
isdigit() |
Checks whether a character is a decimal digit (0-9). |
isgraph() |
Checks whether a character has a graphical representation. |
islower() |
Checks whether a character is a lowercase letter. |
isprint() |
Checks whether a character is printable (including space). |
ispunct() |
Checks whether a character is a punctuation character. |
isspace() |
Checks whether a character is a whitespace character (space, newline, etc.). |
isupper() |
Checks whether a character is an uppercase letter. |
isxdigit() |
Checks whether a character is a hexadecimal digit (0-9, a-f, A-F). |
Character Modification Functions
The <ctype.h>
library also provides functions for converting characters to upper or lower case:
Function | Description |
---|---|
tolower() |
Converts a character to its lowercase equivalent (if it's an uppercase letter). |
toupper() |
Converts a character to its uppercase equivalent (if it's a lowercase letter). |
Example Usage
Here is an example of how to use some of the functions from the <ctype.h>
library:
Example: Using <ctype.h>
Functions
#include <stdio.h>
#include <ctype.h>
int main() {
char ch = 'A';
// Check if the character is uppercase
if (isupper(ch)) {
printf("%c is an uppercase letter.\n", ch);
}
// Convert character to lowercase
char lower = tolower(ch);
printf("Lowercase version of %c is %c.\n", ch, lower);
return 0;
}
Output:
Output
A is an uppercase letter.
Lowercase version of A is a.
In this example, the program checks if the character ch
is an uppercase letter using isupper()
. Then, it converts the uppercase letter to lowercase using tolower()
.