Open In App

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

Last Updated : 16 Dec, 2019
Improve
Improve
Like Article
Like
Save
Share
Report

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:

  1. Printable Characters: The characters that are displayed on the terminal.
  2. 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.

S.No Function Description Return Values
1. isalnum() This function identifies the alphanumeric characters Returns 0 if the passed argument is non – alphanumeric character
Returns non zero value if the passed argument is alphanumeric character
2. isalpha() This function identifies the alphabets from other characters Returns 0 if the passed argument is not an alphabet
Returns non zero value if the passed argument is an alphabet
3. isblank() This function identifies the blank spaces from other characters Returns 0 if the passed argument is not a blank space
Returns nonzero value if the passed argument is a blank space
4. iscntrl() This function identifies the control characters(\n, \b, \t, \r). Returns 0 if the passed argument is not a control character
Returns nonzero value if the passed argument is a control character
5. isdigit() This function identifies numbers in character. Returns 0 if the passed argument is not a number
Returns nonzero value if the passed argument is a number
6. islower() This function identifies the lowercase alphabets. Returns 0 if the passed argument is not a lowercase alphabet
Returns nonzero value if the passed argument is a lowercase alphabet
7. isprint() This function identifies the printable characters. Returns 0 if the passed argument is a non printable character
Returns nonzero value if the passed argument is a printable character
8. ispunct() This function identifies punctuation characters (characters that are neither alphanumeric nor space). Returns 0 if the passed argument is not a punctuation character
Returns nonzero value if the passed argument is a punctuation character
9. isspace() This function identifies white-space characters. Returns 0 if the passed argument is not a white-space character
Returns nonzero value if the passed argument is a white-space character
10. isupper() This function identifies the uppercase alphabets. Returns 0 if the passed argument is not an uppercase alphabet
Returns nonzero value if the passed argument is an uppercase alphabet
11. isxdigit() This function identifies the hexadecimal digit. Returns 0 if the passed argument is not a hexadecimal digit
Returns nonzero value if the passed argument is an hexadecimal digit
12. tolower() This function converts uppercase alphabet to lowercase alphabet. Returns lowercase alphabet of the corresponding uppercase alphabet
13. toupper() This function convert lowercase alphabet to uppercase alphabet. Returns uppercase alphabet of the corresponding lowercase alphabet

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
    
  • 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
    
  • 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.
    


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads