Program to Check Whether a Given Character is an Alphabet, Digit or Any Special Character

Posted in

Program to Check Whether a Given Character is an Alphabet, Digit or Any Special Character
vinaykhatri

Vinay Khatri
Last updated on April 14, 2024

    Problem Statement

    We need to write a script or program that checks whether the user-entered character is an alphabet, digit, or any special character.

    For Example

    Input: 
    B
    Output:
    It is an alphabet

    Approach 1

    In our first approach, we will use the comparison operator to check whether the entered character lies between the alphabets or digits. If it does, we will print; the character is an alphabet or digit. If the character is not an alphabet or digit, we will print that the character is a special symbol.

    C Program to check whether a given character is an alphabet, digit or any special character

    #include <stdio.h>
    
    int main()
    {	
    	char ch;
    	// input the chracter
    	printf("Enter a character: " );
    	scanf("%c",&ch);
    	
    	//check if the character lies between a to z or A to Z
    	if((ch>='A'&& ch<='Z')||(ch>='a' && ch<='z'))
                    printf("It is an alphabet"); 
          
           else 
           { 
                    // check if the charcter lies between 0 to 9 
                    if (ch>='0'&&ch<='9')
    			printf("It is a digit");
    		// if none of the above condition is not true 
    		//this means the character is a special symbol
    		else
    			printf("It is a special character");
    	}
        return 0;
    }

    Output

    Enter a character: A
    It is an alphabet

    C++ Program to check whether a given character is an alphabet, digit or any special character

    #include<iostream>
    using namespace std;
    int main()
    {
    	char ch;
    	// input the character 
    	cout<<"Enter a character: " ; 
            cin>>ch;
    	
    	//check if the character lies between a to z or A to Z
    	if((ch>='A'&& ch<='Z')||(ch>='a' && ch<='z'))
    		cout<<"It is an alphabet"; 
            else 
            { 
                    // check if the character lies between 0 to 9 
                    if (ch>='0'&&ch<='9')
    			cout<<"It is a digit";
    		
                    // if none of the above condition is not true 
    		//this means the character is a special symbol
    		else
    			cout<<"It is a special character";
    	}
    	return 0; 
    }

    Output:

    Enter a character: 4
    It is a digit

    Python Program to check whether a given character is an alphabet, digit or any special character

    # input character
    ch = input("Enter a character: ")
    
    # check for the alphabets
    # check if the character lies between a to z or A to Z
    if (ch>='a' and ch<='z') or (ch>='A' and ch<='Z'):
        print("It is an alphabet")
    
    else: 
        # check if the charcter lies between 0 to 9 
        if (ch>='0' and ch<='9'):
            print("It is a digit")
        else:
            # if none of the above condition is not true
            # this means the character is a special symbol
            print("It is a special character ")

    Output:

    Enter a character: $
    It is a special character

    Approach 2:

    To identify whether the character is an Alphabet, Digit, or Special symbol we will use its ASCII code value. Every character in the programming languages has a unique positive integer ASCII value, and it remains the same for all the programming languages.

    For example

    1. The ASCII code value for alphabets

    • for UpperCase alphabets [A-Z] = [65-90]
    • for LowerCase alphabets [a-z] = [97-122]

    2. The ASCII code value for digits

    • from [0-9] = [48- 57]

    3. And rest all of the ASCII code represent special symbols

    C Program to check whether a given character is an alphabet, digit, or any special character (Using ASCII code)

    #include <stdio.h>
    
    int main()
    {	
    	char ch;
    	int ascii_code;
    	
    	// input the chracter
    	printf("Enter a character: " );
    	scanf("%c",&ch);
    	//convert the character into ascii code 
    	ascii_code = ch;
    	
    	//check if the character lies between [65-90] or [97-122]
    	if((ascii_code>= 65 && ascii_code<=90)||(ascii_code>=97 && ascii_code<=122)) 
                    printf("It is an alphabet"); 
            else 
            { 
                   // check if the charcter lies between [48-57]
                   if (ascii_code>=48 &&ascii_code<=57)
    			printf("It is a digit");
    		// if none of the above condition is not true 
    		//this means the character is a special symbol
    		else
    			printf("It is a special character");
    	}
    }

    Output

    Enter a character: R
    It is an alphabet

    C++ Program to check whether a given character is an alphabet, digit, or any special character (Using ASCII code)

    #include<iostream>
    
    using namespace std;
    int main()
    {
    	char ch;
    	int ascii_code;
    	
    	// input the chracter
    	cout<<"Enter a character: " ; 
            cin>>ch;
    	
    	//convert the character into ascii code 
    	ascii_code = ch;
    	
    	//check if the chracter lies between [65-90] or [97-122]
    	if((ascii_code>= 65 && ascii_code<=90)||(ascii_code>=97 && ascii_code<=122))
    		cout<<"It is an alphabet"; 
            else 
            { 
                    // check if the charcter lies between [48-57]
                    if (ascii_code>=48 &&ascii_code<=57)
    			cout<<"It is a digit";
    		// if none of the above condition is not true 
    		//this means the character is a special symbol
    		else
    			cout<<"It is a special character";
    }
    	return 0; 
    }

    Output

    Enter a character: $
    It is a special character

    Python Program to check whether a given character is an alphabet, digit, or any special character (Using ASCII code)

    # input character
    ch = input("Enter a character: ")
    
    # convert the character into ascii code
    ascii_code = ord(ch)
    
    # check for the alphabets
    # check if the character lies between [65-90] or [97-122]
    if (ascii_code>=65 and ascii_code<=90) or (ascii_code>=97 and ascii_code<=122):
        print("It is an alphabet") 
    else: 
        # check if the charcter lies between [48-57] 
        if (ascii_code>=48 and ascii_code<=57):
            print("It is a digit")
        else:
            # if none of the above condition is not true
            # this means the character is a special symbol
            print("It is a special character ")

    Output

    Enter a character: %
    It is a special character

    Wrapping UP!

    Now let's wrap up this programming tutorial on " Write a program to check whether a given character is an alphabet, digit or any special character". To solve the problem, we have used two approaches, but if we break both approaches, they are the same.

    Although approach 1 uses a comparison operator directly on the character, behind the scene, it is also looking for the character ASCII code for comparison. In Programming interviews , you may face this question where the interview may ask you to use ASCII code to identify the character.

    People are also reading:

    Leave a Comment on this Post

    0 Comments