ctype.h(<cctype>) library in C/C++ with Examples

16 Dec, 2019
As string.h header file contains inbuilt functions to handle Strings in C/C++, the ctype.h/<cctype> contains inbuilt functions to handle characters in C/C++ respectively.
Characters are of two types:
Printable Characters: The characters that are displayed on the terminal.Control Characters: The characters that are initiated to perform a specific operation.
Printable Characters: The characters that are displayed on the terminal.
Control Characters: The characters that are initiated to perform a specific operation.
The arguments passed to character functions should be of integer type. If we pass characters instead of an integer, the characters are typecasted into integers(corresponding ASCII values) and those integers are passed as arguments.
The below functions under ctype.h/<cctype> header file are applied on normal characters. Wide character functions are used for the characters of type wchar_t.
Below are examples to implement some of the above functions:
Example 1: The following program identifies the number of alphabets, digits:#include <stdio.h>  // Header file containing character functions#include <ctype.h>  void identify_alpha_numeric(char a[]){    int count_alpha = 0, count_digit = 0;    for (int i = 0; a[i] != '\0'; i++) {        // To check the character is alphabet        if (isalpha(a[i]))            count_alpha++;          // To check the character is a digit        if (isdigit(a[i]))            count_digit++;    }    printf("The number of alphabets are %d\n",           count_alpha);    printf("The number of digits are %d",           count_digit);}  int main(){    // String Initialization    char a[]        = "Hi 1234, "          " Welcome to GeeksForGeeks";    identify_alpha_numeric(a);}Output:The number of alphabets are 24
The number of digits are 4

#include <stdio.h>  // Header file containing character functions#include <ctype.h>  void identify_alpha_numeric(char a[]){    int count_alpha = 0, count_digit = 0;    for (int i = 0; a[i] != '\0'; i++) {        // To check the character is alphabet        if (isalpha(a[i]))            count_alpha++;          // To check the character is a digit        if (isdigit(a[i]))            count_digit++;    }    printf("The number of alphabets are %d\n",           count_alpha);    printf("The number of digits are %d",           count_digit);}  int main(){    // String Initialization    char a[]        = "Hi 1234, "          " Welcome to GeeksForGeeks";    identify_alpha_numeric(a);}
The number of alphabets are 24
The number of digits are 4

Example 2: The following program identifies the number of uppercase and lowercase alphabets and converts the uppercase to lowercase:#include <stdio.h>  // Header file containing character functions#include <ctype.h>  char* identify_convert_ul(char a[]){    int count_upper = 0, count_lower = 0;    for (int i = 0; a[i] != '\0'; i++) {          // To check the uppercase characters        if (isupper(a[i])) {            count_upper++;            a[i] = tolower(a[i]);        }          // To check the lowercase characters        else if (islower(a[i])) {            count_lower++;            a[i] = toupper(a[i]);        }    }    printf("No. of uppercase characters are %d\n",           count_upper);    printf("No. of lowercase characters are %d",           count_lower);    return a;}  int main(){    // String Initialization    char a[] = "Hi, Welcome to GeeksForGeeks";    char* p;    p = identify_convert_ul(a);    printf("%s", p);}Output:No. of uppercase alphabets are 5
No. of lowercase alphabets are 19
hI, wELCOME TO gEEKSfORgEEKS

#include <stdio.h>  // Header file containing character functions#include <ctype.h>  char* identify_convert_ul(char a[]){    int count_upper = 0, count_lower = 0;    for (int i = 0; a[i] != '\0'; i++) {          // To check the uppercase characters        if (isupper(a[i])) {            count_upper++;            a[i] = tolower(a[i]);        }          // To check the lowercase characters        else if (islower(a[i])) {            count_lower++;            a[i] = toupper(a[i]);        }    }    printf("No. of uppercase characters are %d\n",           count_upper);    printf("No. of lowercase characters are %d",           count_lower);    return a;}  int main(){    // String Initialization    char a[] = "Hi, Welcome to GeeksForGeeks";    char* p;    p = identify_convert_ul(a);    printf("%s", p);}
No. of uppercase alphabets are 5
No. of lowercase alphabets are 19
hI, wELCOME TO gEEKSfORgEEKS

Example 3: The following program prints each word in a new line:#include <stdio.h>  // Header file containing character functions#include <ctype.h>  char* print_word(char a[]){    for (int i = 0; a[i] != '\0'; i++) {          // Space is replaced        // with control character '\n'        if (isblank(a[i]))            a[i] = '\n';    }    return a;}int main(){    // String Initialization    char a[] = "Hello Everyone."               " Welcome to GeeksForGeeks portal. ";    char* p;    p = print_word(a);    printf("%s", p);}Output:Hello
Everyone.
Welcome
to
GeeksForGeeks
portal.

#include <stdio.h>  // Header file containing character functions#include <ctype.h>  char* print_word(char a[]){    for (int i = 0; a[i] != '\0'; i++) {          // Space is replaced        // with control character '\n'        if (isblank(a[i]))            a[i] = '\n';    }    return a;}int main(){    // String Initialization    char a[] = "Hello Everyone."               " Welcome to GeeksForGeeks portal. ";    char* p;    p = print_word(a);    printf("%s", p);}
Hello
Everyone.
Welcome
to
GeeksForGeeks
portal.

C-Functions
C-String
C Language
Strings
Strings
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.