C program to check if a character is an Alphabet or not

C program to check if a character is an Alphabet or not:

In this post, we will learn how to check if a user-input character is an Alphabet or not. We will write one program that will take one character as an input from the user, check if it is an alphabet and print one message.

I will show you three different ways to write this program.

How to check for an Alphabet:

The english alphabet consists of 26 letters i.e. A to Z. But, in a computer program, we have to check for both uppercase and lowercase letters i.e. A to Z and a to z.

We can compare the input character directly with these letters or we can compare with the ASCII values of these characters.

There is also one inbuilt method defined in the ctype.h header file called isalpha() that can also be used to check if a character is an alphabet or not. Let me show you all of these methods one by one.

Method 1: C program to check for an alphabet by comparing with the letters:

The following program checks if an user-input letter is an alphabet or not.

#include <stdio.h>

int main()
{
	char c;
	printf("Enter a character: ");
	scanf("%c", &c);

	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);
}

In this program,

  • c is a character variable. The program asks the user to enter a character. It uses scanf to read the user-input character and assigns it to c.
  • The if block checks if the character is an alphabet or not. It compares the value of c to check if it is in between ‘a’ to ‘z’ or ‘A’ to ‘Z’. If yes, it prints a message that it is an alphabet. Else, it moves to the else block and prints that it is not an alphabet.

It will print output as like below:

Enter a character: e
e is an Alphabet

Enter a character: &
& is not an Alphabet

Method 2: C program to check for an alphabet by comparing with the ASCII values:

We can also write the same program by comparing the given character with the ASCII values of a, A, z and Z. The ASCII values of a, A, z and Z are 97, 65, 122 and 90 respectively. We can compare the input character with these values to check if the entered character is an alphabet or not.

Let’s change the above program to compare the input character with ASCII values:

#include <stdio.h>

int main()
{
	char c;
	printf("Enter a character: ");
	scanf("%c", &c);

	if ((c >= 97 && c <= 122) || (c >= 65 && c <= 90))
		printf("%c is an Alphabet\n", c);
	else
		printf("%c is not an Alphabet\n", c);
}

It will print similar output.

Enter a character: f
f is an Alphabet

Enter a character: Z
Z is an Alphabet

Enter a character: *
* is not an Alphabet

C example program to check for alphabet

Method 3: C program to check for an alphabet by using the isalpha() method:

The isalpha() method is an inbuilt method defined in the ctype.h header file. This method can be used to check if a character is an alphabet or not. It is defined as like below:

int isalpha(int arg)

This method takes one integer argument and returns one integer value. It returns zero if the parameter arg is not an alphabet. Else, it returns a non-zero number.

We can also pass a character as the parameter. It will cast it to integer.

Let’s use this method to check if a character is an alphabet or not:

#include <stdio.h>
#include <ctype.h>

int main()
{
	char c;
	printf("Enter a character: ");
	scanf("%c", &c);

	if (isalpha(c))
		printf("%c is an Alphabet\n", c);
	else
		printf("%c is not an Alphabet\n", c);
}
  • We have to include ctype.h header as the isalpha() method is defined in it.
  • If the return value of isalpha() is not zero, it will run the if block. Else, it will enter to the else block.

It will print similar output.

Enter a character: t
t is an Alphabet

Enter a character: #
# is not an Alphabet

You might also like: