C Standard Library -<ctype.h>
Introduction
C Standard Library'sctype.hThe header file provides some functions that can be used to test and convert characters. These functions are mainly used to check character types (such as letters, digits, whitespace characters, etc.) and to perform character case conversion.
<ctype.h>It provides a convenient set of functions for handling character classification and conversion operations, and is an important tool for character operations in the C standard library.
The following is a simple example demonstrating how to use<ctype.h>Functions provided:
Example
#include <ctype.h>
int main() {
char ch;
// Example characters
char chars[] = "a1 B? \n";
// Check the type of each character
for (int i = 0; chars[i] != '\0'; i++) {
ch = chars[i];
printf("Character: '%c'\n", ch);
if (isalpha(ch)) {
printf(" - isalpha: Yes\n");
} else {
printf(" - isalpha: No\n");
}
if (isdigit(ch)) {
printf(" - isdigit: Yes\n");
} else {
printf(" - isdigit: No\n");
}
if (isspace(ch)) {
printf(" - isspace: Yes\n");
} else {
printf(" - isspace: No\n");
}
if (isprint(ch)) {
printf(" - isprint: Yes\n");
} else {
printf(" - isprint: No\n");
}
if (ispunct(ch)) {
printf(" - ispunct: Yes\n");
} else {
printf(" - ispunct: No\n");
}
}
// Character case conversion example
char lower = 'a';
char upper = 'A';
printf("tolower('%c') = '%c'\n", upper, tolower(upper));
printf("toupper('%c') = '%c'\n", lower, toupper(lower));
return 0;
}
The output result is:
Character: 'a'
- isalpha: Yes
- isdigit: No
- isspace: No
- isprint: Yes
- ispunct: No
Character: '1'
- isalpha: No
- isdigit: Yes
- isspace: No
- isprint: Yes
- ispunct: No
Character: ' '
- isalpha: No
- isdigit: No
- isspace: Yes
- isprint: Yes
- ispunct: No
Character: 'B'
- isalpha: Yes
- isdigit: No
- isspace: No
- isprint: Yes
- ispunct: No
Character: '?'
- isalpha: No
- isdigit: No
- isspace: No
- isprint: Yes
- ispunct: Yes
Character: '
'
- isalpha: No
- isdigit: No
- isspace: Yes
- isprint: No
- ispunct: No
tolower('A') = 'a'
toupper('a') = 'A'
Library functions
The functions defined in the header file ctype.h are listed below.
These functions are used to test whether a character belongs to a certain type. They acceptintas an argument, whose value must be EOF or representable as an unsigned char.
If the argument c satisfies the described condition, these functions return nonzero (true). If the argument c does not satisfy the described condition, these functions return zero.
| Serial Number | Function & Description |
|---|---|
| 1 | int isalnum(int c) This function checks whether the passed character is a letter or a digit. |
| 2 | int isalpha(int c) This function checks whether the passed character is a letter. |
| 3 | int iscntrl(int c) This function checks whether the passed character is a control character. |
| 4 | int isdigit(int c) This function checks whether the passed character is a decimal digit. |
| 5 | int isgraph(int c) This function checks whether the passed character has a graphical representation. |
| 6 | int islower(int c) This function checks whether the passed character is a lowercase letter. |
| 7 | int isprint(int c) This function checks whether the passed character is printable. |
| 8 | int ispunct(int c) This function checks whether the passed character is a punctuation character. |
| 9 | int isspace(int c) This function checks whether the passed character is a whitespace character. |
| 10 | int isupper(int c) This function checks whether the passed character is an uppercase letter. |
| 11 | int isxdigit(int c) This function checks whether the passed character is a hexadecimal digit. |
The standard library also includes two conversion functions that accept and return an "int".
| Serial Number | Function & Description |
|---|---|
| 1 | int tolower(int c) This function converts an uppercase letter to a lowercase letter. |
| 2 | int toupper(int c) This function converts a lowercase letter to an uppercase letter. |
character class
| Serial Number | Character classes & description |
|---|---|
| 1 | Numbers Complete set of digits { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 } |
| 2 | Hexadecimal digits Set { 0 1 2 3 4 5 6 7 8 9 A B C D E F a b c d e f } |
| 3 | Lowercase letters Set { a b c d e f g h i j k l m n o p q r s t u v w x y z } |
| 4 | Uppercase letters Set {A B C D E F G H I J K L M N O P Q R S T U V W X Y Z } |
| 5 | Letter Set of lowercase and uppercase letters |
| 6 | Alphanumeric character The set of digits, lowercase letters, and uppercase letters. |
| 7 | Punctuation character Set ! " # $ % & ' ( ) * + , - . / : ; < = > ? @ [ \ ] ^ _ ` { | } ~ |
| 8 | Graphic character The set of alphanumeric characters and punctuation characters. |
| 9 | Space character The set of tab, newline, vertical tab, form feed, carriage return, and space characters. |
| 10 | Printable character The set of alphanumeric characters, punctuation characters, and space characters. |
| 11 | Control character In ASCII encoding, the octal codes for these characters are from 000 to 037, as well as 177 (DEL). |
| 12 | whitespace character Includes space and tab characters. |
| 13 | Alphabetic character Set of lowercase and uppercase letters. |