C ctype.h Library Functions Programs (Set 1)

The <ctype.h> header file of the C Standard Library has several built-in functions that are useful for testing and mapping characters.

C ctype.h Library Functions

This section contains solved programs on ctype.h header file's library functions. The 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 C ctype.h Library Functions Programs

1) isalnum()

This function checks whether character is alphanumeric or not.

Example of isalnum() function

/* 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;
}

Output:

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.

Example of isalpha() function

/* 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;
}

Output:

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.

Example of isdigit() function

/* 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;
}

Output:

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.

Example of isspace(), isupper(), and islower() functions

/* 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;
}

Output:

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 , . : ; ` @ # $ % ^ & * ( ) < > [ ] \ / { } ! | ~ - _ + ? = ' "

Example of ispunct() function

/* 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;
}

Output:

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.

Example of isprint() function

/* 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;
}

Output:

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.

Example of toupper() and tolower() functions

/* 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;
}

Output:

First run:
Enter a character: w  
Upper case: W, Lower case: w

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

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.