Inthis C++ Example, we’ll learn How to Check Whether a character is Vowel or Consonant in C++.
if…else statement is used to check whether an alphabet entered by the user is a vowel or a constant.
The character entered by the user is stored in variable c.
The isLowerCaseVowel evaluates to true if c is a lowercase vowel and false for any other character.
Similarly, isUpperCaseVowel evaluates to true if c is an uppercase vowel and false for any other character.
If both isLowercaseVowel and isUppercaseVowel is true, the character entered is a vowel , if not the character is a consonant.
C++ Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
#include <iostream> using namespace std; int main() { char c; int isLowercaseVowel, isUppercaseVowel; cout << "Enter an alphabet: "; cin >> c; // evaluates to 1 (true) if c is a lowercase vowel isLowercaseVowel = (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u'); // evaluates to 1 (true) if c is an uppercase vowel isUppercaseVowel = (c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U'); // evaluates to 1 (true) if either isLowercaseVowel or isUppercaseVowel is true if (isLowercaseVowel || isUppercaseVowel) cout << c << " is a vowel."; else cout << c << " is a consonant."; return 0; } |
Output: