C program to check if a character is alphabet or not


Explore a C program that efficiently checks if a character is an alphabet or not. Learn how to utilize ASCII character representations and comparison operators in C to determine whether a given character falls within the range of uppercase or lowercase alphabets.

In this article, we'll explore how to create a simple C program to check whether a given character is an alphabet or not.

Understanding Alphabets in ASCII

In the ASCII (American Standard Code for Information Interchange) character set, alphabets are represented by contiguous ranges of values. Uppercase letters span from 'A' to 'Z' (65 to 90 in ASCII), while lowercase letters span from 'a' to 'z' (97 to 122 in ASCII).

C program to check if a character is alphabet or not

Let's delve into the C programming language to create a program that checks whether a user-input character is an alphabet.

#include <stdio.h>

int main() {
    char ch;

    // Input from the user
    printf("Enter a character: ");
    scanf(" %c", &ch);

    // Checking if the character is an alphabet
    if ((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z')) {
        printf("%c is an alphabet.", ch);
    } else {
        printf("%c is not an alphabet.", ch);
    }

    return 0;
}

Output

Enter a character: a
a is an alphabet.

The program checks if the character falls within the ASCII range of uppercase or lowercase alphabets. If the character is within these ranges, it is identified as an alphabet.

In this article, we've created a simple C program that efficiently determines whether a given character is an alphabet or not. Understanding ASCII character representations and utilizing comparison operators in C enables us to accurately identify alphabets from other characters.


Recommended Posts