Table of Contents
Introduction
In this C program, we will explore how to check if a character is an alphabet or not.
On the input screen, the user needs to enter a character value to check whether it is an alphabet or a non-alphabet character.
To understand this program better, you should know about
1. C programming operators
2. C nested if…else statement
The third and easiest way to check if the entered character is an alphabet or not is by bypassing the character in the isalpha() function, and it will give us the results.
Since isalpha is a predefined C function thus, we need to include ctype library to use it in our code.
Program
#include<stdio.h> #include<ctype.h> int main() { char c; printf("Enter a character:\n"); scanf("%c",&c); if (isalpha(c)) // c is passed in isalpha to check its behaviour { printf("%c ia a an alphabet",c); } else { printf("%c ia a not an alphabet",c); } return 0; }
Output
Explanation
The character value entered by the user is stored in variable ‘c’.
if (isalpha(c))
In the above statement ‘if’ condition is used here, in which we passed the isalpha() function. When ‘c’ as an argument is passed in isalpha(), and it returns true, then the block of the if statement gets executed.
0 Comments