×

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.
2isalpha(c)It is an acronym to test a character as alphabetic and it checks whether the passed character is alphabetic.
3iscntrl(c)Short for control character and it checks whether the passed character is a control character.   (Control character)
4isdigit(c)This function is used to check whether the passed character is a decimal digit.   (decimal digit)
5isgraph(c)This function is used to check whether the passed character has graphical representation using locale.   (printing character except space)
6islower(c)This function is used to check whether the passed character is a lowercase letter.   (lower-case letter)
7isprint(c)This function is used to check whether the passed character is printable.   (printing character except space)
8ispunct(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)
9isspace(c)This function is used to check whether the passed character is white-space.   (space, form feed, newline, carriage return, tab, vertical tab)
10isupper(c)This function is used to check whether the passed character is an uppercase letter.   (upper-case letter)
11isxdigit(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
1Digitsset of whole numbers (range: 0 to 9)    { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }.
2Hexadecimal digitsset 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 }.
3LettersIt 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 }  
4Lowercase lettersIt 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 }.
5Uppercase lettersIt 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 }.
6Alphanumeric charactersIt 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 }
7Alphabetic charactersset of Lowercase letters and Uppercase letters.
8Punctuation charactersset of @ # * ! "  $ % & ' ( )  + , - . / : ; < = > ? @ [ \ ] ^ _ ` { | } ~
9Printable charactersset 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 }  
10Control charactersoctal codes 000 through 037, and 177 (DEL).
11Graphical characters set of Alphanumeric characters and Punctuation characters.   {set of Digits (0-9), Lowercase letters (a-z) and Uppercase letters (A-Z) + punctuation characters}
12Blank charactersspaces and tabs.
13Space charactersset of tab, newline, vertical tab, return, and space etc.

Related Topics

Use of Structure in C

We can usually use arrays in C programming to store elements of the same data type. We can use strings to store multiple elements of a character data type. Still,...

4 minutes read.

Bar3d() function in C Graphics

The bar3d function is used to create a 2-dimensional filled-in rectangular bar. We can also create three-dimensional shapes in C using helpful function. The first step in creating this 3D...

3 minutes read.

How to Avoid Structure Padding in C

Generally, when an object of some structure type is declared, some contiguous memory will be allocated in block form, which will be allocated to structure members. To understand structure padding...

3 minutes read.

Projects on C language in 2024

Introduction Most aspiring programmers learn the C language as their first high-level programming language. The most flexible language utilized in every industry is, without a doubt, C. Even after 50 years...

9 minutes read.

Flow Chart of Do while loop in C

This is a flowchart that represents the process of executing the Do while loop in the C programming language Generally, as we know there are three main components of Do While...

3 minutes read.

memcmp() in C

Introduction: In this article we are discuss about memcmp() function in C. This function permits the person to evaluate the bytes of the two characters, as mentioned above. Depending on...

4 minutes read.

C pre-processor

The C processor is a macro processor which is used to compile the source code of the program (step by step) . It is not a part of the compiler....

4 minutes read.

Difference between If Else and Nested If Else in C

What is the If Else statement in C? In C programs, if else conditions are used to perform some operations based on specific conditions. The operation is only executed if the...

6 minutes read.

Binomial Coefficient Program in C

What is Binomial coefficient? In the given set of n possibilities, the binomial coefficient(n,k) indicates the order of choosing 'K' results from those possibilities. Binomial coeeficient of posistive n and k...

3 minutes read.

Compilation Process in C language

What is the Compilation Process?  The compilation is a method whereby the source code is converted into object code. It is achieved with compiler assistance. The compiler tests the source code for syntactic...

3 minutes read.

Explain the two-way selection in C

In C programming, a two-way selection statement is used to choose between two different options based on a Boolean expression. The two-way selection statement in C is the if statement. The...

6 minutes read.

Stack implementation in C

Stack implementation in C Stack stores the data in a particular order. It is a linear data structure that follows the principle of the Last In First Out (LIFO) technique where...

3 minutes read.

C printf and Scanf

Input-Output functions in C Programming In C Language, the printf() and scanf() are inbuilt library functions that used for input and output. It is defined in the header file“<stdio.h>”. printf() Function: In C...

2 minutes read.

fflush in C

In this article, we will understand what is fflush(), the need for fflush and fflush(stdin), and fflush(stdout). The fflush() function is used to clear the output buffer and move the buffered...

3 minutes read.

Command line arguments in C

Command line arguments in C The arguments that are generally passed from the line of command are referred to as command line arguments. These command line arguments are always handled by...

3 minutes read.

#define in C

#define in C In the C programming language, the preprocessor directive acts an important role within which the #define directive is present that is used to define the constant or the...

3 minutes read.

Pointer in C

Pointer is a variable that is used to contain the address of another variable. We can easily create a pointer in C language. Example: int *ptr Symbol Description & (ampersand sign) Address of an operator...

1 minute read.

Find median of 1D array using function in C

Introduction: Here, we discuss how we can find the median of a 1D array using a function in C. The median is the value in the middle of the sorted...

3 minutes read.

Builtin functions of GCC compiler

Certain built-in functions are present in the GCC compiler. These features are as follows: Function_ built in_popcount (x) The number of 1s in data of the integer type can be counted using...

3 minutes read.

Assignment Operator Program in C

An assignment operator is a symbol used to assign a value to a variable in a programming language. The assignment operator is often the equal symbol (=) in programming languages....

12 minutes read.