Character Class Tests in C
Introduction
The <ctype.h> is a header file in C which is used to declare functions for testing characters. This header file of the C standard library is used to declare multiple functions which are beneficial for not only testing characters but also mapping them.
In the <ctype.h>, each function that has the argument is an int. What this means is, that all the functions accept int as a parameter, whose value must be EOF (End of File) or representable as an unsigned char, and the return value is an int.
Note: If the argument c satisfies the described conditions, then the functions return non-zero (true) and in case it does not satisfy the conditions, the functions return zero(false).
Library Functions
S. No. | Function | Description |
1 | isalnum(c) | It stands for is_alphanumeric() and it checks whether the passed character is alphanumeric or not. |
2 | isalpha(c) | It is an acronym to test a character as alphabetic and it checks whether the passed character is alphabetic. |
3 | iscntrl(c) | Short for control character and it checks whether the passed character is a control character. (Control character) |
4 | isdigit(c) | This function is used to check whether the passed character is a decimal digit. (decimal digit) |
5 | isgraph(c) | This function is used to check whether the passed character has graphical representation using locale. (printing character except space) |
6 | islower(c) | This function is used to check whether the passed character is a lowercase letter. (lower-case letter) |
7 | isprint(c) | This function is used to check whether the passed character is printable. (printing character except space) |
8 | ispunct(c) | This function is used to check whether the passed character is a punctuation character. (It prints characters with the exception of space or letter or digit) |
9 | isspace(c) | This function is used to check whether the passed character is white-space. (space, form feed, newline, carriage return, tab, vertical tab) |
10 | isupper(c) | This function is used to check whether the passed character is an uppercase letter. (upper-case letter) |
11 | isxdigit(c) | This function is used to check whether the passed character is a hexadecimal digit. (hexadecimal digit) |
In C programming, the seven bit ASCII character set consists of ;
- The printing characters which lies in the range : 0 x 20 (‘ ‘) to 0 x 7E (‘ ~ ‘)
- And, The control characters which lies in the range : 0 (NULL) to 0 x 1F(US), and 0x7F(DEL)
Note: There are a few special cases, and it consists of two functions that converts the case of letter (either uppercase or lowercase):
int tolower (int c) | Converts c to lowercase |
int toupper (int c) | Converts c to uppercase |
Here, one thing to keep a note of is,
- If ‘c’ is a lowercase letter, toupper(c) returns the corresponding upper-case letter; otherwise, it returns c.
- If ‘c’ is an uppercase letter, tolower(c) returns the corresponding lower-case letter; otherwise, it returns c.
Character Classes
S. No. | Function | Description |
1 | Digits | set of whole numbers (range: 0 to 9) { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }. |
2 | Hexadecimal digits | set of whole numbers + alphabets ranging from A to F or a to f (both cases) { 0 1 2 3 4 5 6 7 8 9 A B C D E F a b c d e f }. |
3 | Letters | It is a set or collection of letters from both cases. In simpler terms, it is a set of lowercase and uppercase letters. The range of letters lies from: (a to z) and (A to Z) { 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 } + {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 | Lowercase letters | It is a set of lowercase letters which lies in the range from (a to z) { 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 | Uppercase letters | It is a set of uppercase letters which lies in the range from: (A to Z) {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 }. |
6 | Alphanumeric characters | It is a set of Digits (0-9) , Lowercase letters (a-z) and Uppercase letters (A-Z). { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 + a to z + A to Z } |
7 | Alphabetic characters | set of Lowercase letters and Uppercase letters. |
8 | Punctuation characters | set of @ # * ! " $ % & ' ( ) + , - . / : ; < = > ? @ [ \ ] ^ _ ` { | } ~ |
9 | Printable characters | set of Alphanumeric characters, Space characters. and Punctuation characters {set of Digits (0-9) , Lowercase letters (a-z) and Uppercase letters (A-Z) + punctuation characters + space characters } |
10 | Control characters | octal codes 000 through 037, and 177 (DEL). |
11 | Graphical characters | set of Alphanumeric characters and Punctuation characters. {set of Digits (0-9), Lowercase letters (a-z) and Uppercase letters (A-Z) + punctuation characters} |
12 | Blank characters | spaces and tabs. |
13 | Space characters | set of tab, newline, vertical tab, return, and space etc. |