<ctype.h> header file library functions example programs.

This section contains example programs on ctype.h header file’s library functions. ctype.h header file contains the functions related to characters. Some of the useful library functions are: isalnum(), isalpha(), isdigit(), isspace(), ispunct(), toupper(), tolower().

List of ctype.h header file’s library functions with explanation and example programs

1) isalnum()

This function checks whether character is alphanumeric or not.

/* C example program of isalnum().*/
#include<stdio.h>
#include<ctype.h>

int main()
{
    char ch;
    
    printf("Enter a character: ");
    scanf("%c",&ch);
    
    if(isalnum(ch))
        printf("%c is an alphanumeric character.\n",ch);
    else
        printf("%c is not an alphanumeric character.\n",ch);
        
    return 0;
}
    First run:
    Enter a character: H  
    H is an alphanumeric character.

    Second run:
    Enter a character: %  
    % is not an alphanumeric character.  



2) isalpha()

This function checks whether character is alphabet or not.

/* C example program of isalpha().*/
#include<stdio.h>
#include<ctype.h>

int main()
{
    char ch;
    
    printf("Enter a character: ");
    scanf("%c",&ch);
    
    if(isalpha(ch))
        printf("%c is an alphabet.\n",ch);
    else
        printf("%c is not an alphabet.\n",ch);
        
    return 0;
}
    First run:
    Enter a character: Y  
    Y is an alphabet.  

    Second run:
    Enter a character: 9  
    9 is not an alphabet. 



3) isdigit()

This function checks whether character is digit or not.

/* C example program of isdigit().*/
#include<stdio.h>
#include<ctype.h>

int main()
{
    char ch;
    
    printf("Enter a character: ");
    scanf("%c",&ch);
    
    if(isdigit(ch))
        printf("%c is a digit.\n",ch);
    else
        printf("%c is not a digit.\n",ch);
        
    return 0;
}
    First run:
    Enter a character: Y  
    Y is not a digit.  

    Second run:
    Enter a character: 9  
    9 is a digit.



4) isspace()

This function checks whether character is space or not.

5) isupper()

This function checks whether character is an uppercase character or not.

6) islower()

This function checks whether character is a lowercase character or not.

/* C example program of isspace(), isupper(), islower() .*/
#include<stdio.h>
#include<ctype.h>

int main()
{
    char ch;
    
    printf("Enter a character: ");
    scanf("%c",&ch);
    
    if(isupper(ch))
        printf("%c is an uppercase character.\n",ch);
    else if(islower(ch))
        printf("%c is an lowercase character.\n",ch);
    else if(isspace(ch))
        printf("%c is space.\n",ch);
    else
        printf("%c is none from uppercase, lowercase and space.\n",ch);
        
    return 0;
}
    First run:
    Enter a character: T  
    T is an uppercase character.

    Second run:
    Enter a character: t  
    t is an lowercase character.

    Third run:
    Enter a character: main  
    m is an lowercase character.

    Fourth run:
    Enter a character: 
      is space.  

    Fifth run:
    Enter a character: *  
    * is none from uppercase, lowercase and space.  



7) ispunct()

This function checks whether character is a punctuation character or not.
Punctuation characters are , . : ; ` @ # $ % ^ & * ( ) < > [ ] \ / { } ! | ~ - _ + ? = ' "

/* C example program of ispunct() .*/
#include<stdio.h>
#include<ctype.h>

int main()
{
    char ch;
    
    printf("Enter a character: ");
    scanf("%c",&ch);
 
    if(ispunct(ch))
        printf("%c is a punctuation character.\n",ch);
    else
        printf("%c is not a punctuation character.\n",ch);
 
    return 0;
}
    First run:
    Enter a character: !  
    ! is a punctuation character.  

    Second run:
    Enter a character: ,  
    , is a punctuation character.  

    Third run:
    Enter a character: +  
    + is a punctuation character.  



8) isprint()

This function checks whether character is printable or not.

/* C example program of ispunct() .*/
#include<stdio.h>
#include<ctype.h>

int main()
{
    char ch;
    
    printf("Enter a character: ");
    scanf("%c",&ch);
    
    if(isprint(ch))
        printf("%c is a printable character.\n",ch);
    else
        printf("%c is not a printable character.\n",ch);

    return 0;
}
    Enter a character: x  
    x is a printable character.



9) toupper()

This function returns character in upper case.

10) tolower()

This function returns character in lower case.

/* C example program of toupper() and tolower() .*/
#include<stdio.h>
#include<ctype.h>

int main()
{
    char ch;
    
    printf("Enter a character: ");
    scanf("%c",&ch);
 
    printf("Upper case: %c, Lower case: %c\n",toupper(ch),tolower(ch));
 
    return 0;
}
    First run:
    Enter a character: w  
    Upper case: W, Lower case: w

    Second run:
    Enter a character: 9  
    Upper case: 9, Lower case: 9