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

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:
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    #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);
    }

    chevron_right

    
    

    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:
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    #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);
    }

    chevron_right

    
    

    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:
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    #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);
    }

    chevron_right

    
    

    Output:

    Hello
    Everyone.
    Welcome
    to
    GeeksForGeeks
    portal.
    

GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.