C++ Program to Check Whether a Character is Alphabet or Not
ADVERTISEMENTS
C++ program to check whether a character is alphabet or not. In this article, you will learn how to check whether a character is alphabet or not in c++ 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++
cin
object - C++
cout
object
C++ Program to Check Whether a Character is Alphabet or Not using If-else
// C++ Program to Check Whether a Character is Alphabet or Not using If-else
#include <iostream>
using namespace std;
int main() {
char x; // To store the input character
cout << "-----Enter a character to check itself-----\n";
cin >> x;
if ((x >= 'a' && x <= 'z') || (x >= 'A' && x <= 'Z')) {
cout << "\n" << x << " is an alphabet character.\n";
} else {
cout << "\n" << x << " is not an alphabet character.\n";
}
return 0;
}
Output
-----Enter a character to check itself-----
K
K is an alphabet character.
C++ Program to Check Whether a Character is Alphabet or Not using Ternary Operator
// C++ Program to Check Whether a Character is Alphabet or Not using Ternary Operator
#include <iostream>
using namespace std;
int main() {
char x, f;
// x - To store the input character
cout << "-----Enter a character to check itself-----\n";
cin >> x;
f = (x >= 'a' && x <= 'z') || (x >= 'A' && x <= 'Z') ? 1 : 0;
if (f) {
cout << "\n" << x << " is an alphabet character.\n";
} else {
cout << "\n" << x << " is not an alphabet character.\n";
}
return 0;
}
Output
-----Enter a character to check itself-----
9
9 is not an alphabet character.
Explanation
In this program, we have taken input K
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.