BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

C Program to check whether a Character is an Alphabet or not

By Chaitanya Singh | Filed Under: C Programs

In this tutorial, you will learn how to write a C program to check whether a character entered by user is an alphabet or not.

Example: Program to check whether a character is an Alphabet or not

In the following example, user is asked to enter a character, which is stored in a char variable ch. Once the value of entered character is captured using scanf() function, you can easily check whether its an alphabet or not by doing the following check in if statement.

For lowercase alphabets: >= ‘a’ and <= ‘z’
For uppercase alphabets: >= ‘A’ and <= ‘Z’

If the input character lies in one of these ranges then the character is an alphabet else it’s not an alphabet.

#include <stdio.h>
int main() {
  char ch;
  printf("Enter a character: ");
  scanf("%c", &ch);

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

  return 0;
}

Output:
When the entered character is ‘A’
C Program to Check Whether a Character is an Alphabet or not

When the entered character is 9
C Program to Check Whether a Character is an Alphabet or not

You can also check the alphabet using the ASCII values of characters like this:

if((ch >= 97 && ch <= 122) || (ch >= 65 && ch <= 90))
   printf("The entered character %c is an Alphabet",ch);
else
   printf("The entered character %c is not an Alphabet",ch);

The ASCII value of ‘a’ is 97, ‘z’ is 122, ‘A’ is 65 and ‘Z’ is 90.

Related C Examples:

  • C Program to check whether an alphabet is vowel or consonant
  • C Program to find ASCII value of a Character
  • C Program to sort set of strings in alphabetical order
  • C Program to reverse a string
❮ C TutorialC Programs ❯

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Programs

  • C Programs
  • Java Programs
  • C++ Programs

Copyright © 2012 – 2022 BeginnersBook . Privacy Policy . Sitemap