PRO
Albiorn
asked

Expert
Kelish Rai answered
Here's how you can check with an if...else statement whether a value is a character or not:
if ((alphabet >= 'a' && alphabet <= 'z') || (alphabet >= 'A' && alphabet <= 'Z')) {
printf("Alphabet");
} else {
printf("Not an Alphabet");
}Here,
The condition
alphabet >= 'a' && alphabet <= 'z'checks if the character is a lowercase letter (fromatoz).The condition
alphabet >= 'A' && alphabet <= 'Z'checks if it’s an uppercase letter (fromAtoZ).The
||(OR) operator ensures that if either condition is true, the program will print"Alphabet".If neither condition is true, the program prints
"Not an Alphabet"in theelseblock.
To check if it’s a number:
If you want to check if the value is a number (digit 0-9), you can extend the logic like this:
if ((alphabet >= 'a' && alphabet <= 'z') || (alphabet >= 'A' && alphabet <= 'Z')) {
printf("Alphabet");
} else if (alphabet >= '0' && alphabet <= '9') {
printf("Number");
} else {
printf("Special Character");
}This version will now differentiate between alphabets, numbers, and special characters.
C
This question was asked as part of the Practice: C Programming course.
Our Experts
Sudip BhandariHead of Growth/Marketing
Apekchhya ShresthaSenior Product Manager
Kelish RaiTechnical Content Writer
Abhilekh GautamSystem Engineer
Palistha SinghTechnical Content Writer
Sarthak BaralSenior Content Editor
Saujanya Poudel
Abhay Jajodia
Nisha SharmaTechnical Content Writer
Udayan ShakyaTechnical Content Writer