Showing posts with label Program to test the character type. Show all posts
Showing posts with label Program to test the character type. Show all posts

Program to test the character type

Program 12) This program requests the user to enter a character and displays a message on the screen telling the user whether the character is an alphabet or digit, or any other special character.

This program receives a character from the keyboard and tests whether it is a letter or digit and prints out a message accordingly. These tests are done with the help of the following functions:
                                             isalpha(character)
                                              isdigit(character)
For example, isalpha assumes a value non-zero (TRUE) if the argument character contains an alphabet;
otherwise it assumes 0 (FALSE). Similar in the case with the function isdigit.


                                    PROGRAM

#include <stdio.h>
#include <ctype.h>
main( )
 {
        char character ;
        printf("Press any key\n") ;
        
        character = getchar ;
        if(isalpha(character) > 0)
           printf("The character is a letter.") ;
        else
            if(isdigit(character) >0 )
               printf("The character is a digit.") ;
            else
               printf("The character is not alphanumeric.") ;
        getch( ) ;
 }

Output:             Press any key
                         h
                         The character is a letter.
                         Press any key
                         5
                         The character is a digit.
                         Press any key
                         *
                         The character is not alphanumeric.


isalpha and isdigit. These character functions are contained in the file ctype.h and therefore the statement
                         #include <ctype.h>
must be included in the program.