C program to check if a character is an alphabet or not using ternary operator


Efficiently verify alphabet characters in C programming using the conditional operator. Explore a concise method to classify characters as alphabets or non-alphabets.

Understanding Alphabet Characters

Alphabet characters are letters from a to z (uppercase or lowercase) in the English alphabet.

C program to check if a character is an alphabet or not using ternary operator

Let's delve into the C programming language to create a program that checks whether a character is an alphabet or not using the conditional operator.

#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 using conditional operator
    ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')) ? printf("%c is an alphabet.", ch) : printf("%c is not an alphabet.", ch);

    return 0;
}

Output

Enter a character: A
A is an alphabet.

Checking for Alphabet: The program uses the conditional operator to check if the entered character is within the range of lowercase (a to z) or uppercase (A to Z) alphabets. It then displays whether the character is an alphabet or not.

In this article, we've created a simple C program that efficiently determines whether a character is an alphabet or not using the conditional or ternary operator. Leveraging the conditional operator in C programming enables us to streamline conditional checks and classify characters as alphabets or non-alphabets effectively.


Recommended Posts