C Program to Check Whether a Character is an Alphabet or Not
ADVERTISEMENTS
C program to check whether a character is an alphabet or not. In this article, you will learn how to check whether a character is alphabet or not in c programming language.
Example-1
Input: A
A is an alphabet character.
Example-2
Input: 9
9 is not an alphabet character.
You should have knowledge of the following topics in c programming to understand these programs:
- C Ternary operator
- C
main()
function - C
if-else
statement - C
printf()
function
C Program to Check Whether a Character is an Alphabet or Not using If-else
// C Program to Check Whether a Character is an Alphabet or Not using If-else
#include <stdio.h>
int main() {
char x; // Value of the input character
printf("-----Enter a character to check itself-----\n");
scanf("%c", &x);
if ((x >= 'a' && x <= 'z') || (x >= 'A' && x <= 'Z')) {
printf("\n%c is an alphabet character.\n", x);
} else {
printf("\n%c is not an alphabet character.\n", x);
}
return 0;
}
Output
-----Enter a character to check itself-----
g
g is an alphabet character.
C Program to Check Whether a Character is an Alphabet or Not using Conditional Operator
// C Program to Check Whether a Character is an Alphabet or Not using Conditional Operator
#include <stdio.h>
int main() {
char x, f;
// x - Value of the input character
printf("-----Enter a character to check itself-----\n");
scanf("%c", &x);
f = (x >= 'a' && x <= 'z') || (x >= 'A' && x <= 'Z') ? 1 : 0;
if (f) {
printf("\n%c is an alphabet character.\n", x);
} else {
printf("\n%c is not an alphabet character.\n", x);
}
return 0;
}
Output
-----Enter a character to check itself-----
g
g is an alphabet character.
Explanation
In this program, we have taken input g
from the user then it passes to check this character between alphabetic series.
a, b, ..., y, z
A, B, ..., Y, Z
Although the given character does exist in this series or not if it doesn't in this series then the given character is not alphabetic.