Check Whether a Character is Alphabet or Not

Definition & Explanation

To check whether a character is an alphabet or not, you can use the ASCII values of characters. In ASCII, the uppercase letters ‘A’ to ‘Z’ have decimal values from 65 to 90, and the lowercase letters ‘a’ to ‘z’ have decimal values from 97 to 122. So, if the ASCII value of a character falls within these ranges, it is an alphabet; otherwise, it is not.

Program Logic

  1. Check if the ASCII value of the character falls within the range of uppercase letters (‘A’ to ‘Z’) or lowercase letters (‘a’ to ‘z’).
  2. If the condition is true, then the character is an alphabet; otherwise, it is not.
C

Method 1 :

#include <stdio.h>

int main() {
    char c = 'e';

    if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) {
        printf("%c is an Alphabet\n", c);
    } else {
        printf("%c is not an Alphabet\n", c);
    }

    return 0;
}

Output :

C++

Method 1 :

#include <iostream>
using namespace std;

int main() {
    char c = 'e';

    if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) {
        cout << c << " is an Alphabet" << endl;
    } else {
        cout << c << " is not an Alphabet" << endl;
    }

    return 0;
}

Output :

JAVA

Method 1 :

public class Main {
    public static void main(String[] args) {
        char c = 'e';

        if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) {
            System.out.println(c + " is an Alphabet");
        } else {
            System.out.println(c + " is not an Alphabet");
        }
    }
}

Output :

Python

Method 1 :

ch='z'
if 'a'<= ch <='z' or 'A' <=ch <= 'Z':
	print("The character ",ch, "is an Alphabet")
else:
	print("the character",ch,"is not an Alphabet")
	

Output :

The character  z is an Alphabet