alt

Alphabet Letter : Identify a character as Letter

Hello Guys. Welcome you all to my new article on C Progaming – How to do it series. Today I’m going to Read a character from the keyboard and print whether it is an alphabet letter or not.We can do it in the following two methods

  • Meth 01 :- Using only if condition and rest
  • Meth 02 :- Using logical functions and if conditions

Analysing the Question

At the very beginning we have to find what is the logic / condition that hidden in the question. We have to read a character and find whether it is in the alphabet. According   ASCII table we know that we can input 128 characters using key board. When we considering a character if it is an alphabet letter, it must be satisfy one of following conditions.

  •  character value must ” equal or greater than to value of ‘a’ and equal or less than value of ‘z’  ”  – lowercase
  • character value must ” equal or greater than to value of ‘A’ and equal or less than value of ‘A’  ”  – uppercase

In the program we get character as the ‘char’ variable named “ch”. so we can write above conditions as following using parameters in C language.

  • ch>=’a’ and ch<=’z’ for lowercase  (  ch>=’a’ && ch<=’z’ )
  • ch>=A’ and ch<=’Z’ for uppercase  (  ch>=’A’ && ch<=’Z’ )

Method 01 – Using only IF

In this case we use IF condition only. It is little bit complicate. But if know the basic idea and the logic. you can do it simply. Ok. Lets move to our work. Now it’s time to recall the ASCII Values of some characters. They are “A = 65   Z=90  a = 97    z=122 ”  If we can sort the values in this range we can get them as alphabet letters.

Flow Chart

alphabet letter

Source Code

Here is the source code for this program. You can simply copy paste and run the program.

#include <stdio.h>
#include <stdlib.h>

int main()
{
char ch;
printf("Input a character of KeyBoard : ");
scanf("%c",&ch);

if (ch>='A')
{
if (ch<='Z')
printf("%c is a character of Alphabet \n \n",ch); //Defined the Uppercase letters
else
printf(" %c is not a character of alphabet \n \n",ch); //characters between Z and a
}
else
{
if(ch>='a')
{
if (ch<='z')
printf("%c is a character of Alphabet \n \n",ch); //Has defined lowercase letter
else
printf("%c is not a character of Alphabet \n \n",ch); //characters after z
}
else
printf("%c is not a character of Alphabet \n \n",ch); // characters before A
}
printf(" A program by BMaxx @ Digital Chalkie \n Thank you for join with us \n \n\n \n");

return 0;
}

This is the way to ead a character from the keyboard and print whether it is an alphabet or not , using only the if conditon.

Method 02

When we use the logical expressions, we can solve this program easily. In this case we use ‘or’ condition to define the range of each case and ‘and’ expression to sort out the Alphabetic characters.

  • For lowercase  – ch>=’a’ && ch<=’z’ 
  • For uppercase – ch>=’a’ && ch<=’z’ 
  • When combined it –  ch>=’a’ && ch<=’z’  || ch>=’a’ && ch<=’z’ 
Source Code
#include <stdio.h>
int main()
{
    char ch;

    //Asking user to enter the character
    printf("Enter any character: ");
    //storing the entered character into the variable ch
    scanf("%c",&ch);

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

    return 0;
}

Ok guys. It is time to wind up. If there any questions or opinions please mention them below. Hit the like,comment on and share. Good Bye All.

Bonomi Tharinda
Bonomi Tharinda is the Developer of the BMaxx Blog. He is a skilled web developer and graphic designer. In addition, he is an undergraduate student of a representative University in Sri Lanka. He is working as a tutor of an institute and as a part-time blogger, he will help you all to make your problems solved.Here are his social accounts and you can follow him through them.