In this C++ program, we have to check whether the given character is a vowel or consonant.
Among the 26 letters in the alphabet, 5 of which are vowels ( a, e, I, o, u ) and the rest of which are consonants.
To check whether the given character is a vowel or consonant, simply we have to compare the entered character with the vowel letters ( a, e, I, o, u ).
Ask the user to enter a character. Store the character entered by the user into a character type variable c. The entered character maybe
So, we have to check for these three options. Declare Boolean type variables lowercase and uppercase. Boolean is a data type that is declared with the keyword bool
and can take only the values true or false. True = 1 and false = 0.
Isalpha()
is a function in C++ used to check if the given character is an alphabet or not?For checking the if………..elseif…..else
statement can be used
if ( condition 1)
{ code block 1 }
elseif (condition 2)
{ code block 2 }
else
{ code block 3 }.
Step 1: Call the header file iostream
.
Step 2: Use the namespace std.
Step 3: Open the integer type main function; int main().
Step 4: Declare a character type variable c, bool type variables lowercase, uppercase.
Step 5: Print a message “Enter a character”.
Step 6: Read the character to the variable c.
Step 7: check if the character is an alphabet. If false, print a non-alphabetic character.
Step 8: Check the character is an uppercase vowel If true; Print c is a vowel.
Step 9: Check the character is a lower case vowel. If true; Print c is a vowel.
Step 10: If step 7 is true and step 8 and step 9 are false; then print c is consonant.
Step 11: Exit.
#include <iostream>
using namespace std;
int main() {
char c;
bool lowercase, uppercase;
cout << "Enter an alphabet: ";
cin >> c;
// evaluates to 1 (true) if c is a lowercase vowel
lowercase = (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u');
// evaluates to 1 (true) if c is an uppercase vowel
uppercase= (c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U');
// show error message if c is not an alphabet
if (!isalpha(c))
printf("Error! Non-alphabetic character.");
else if (lowercase || uppercase)
cout << c << " is a vowel.";
else
cout << c << " is a consonant.";
return 0;
}
Run 1 Enter an alphabet: E E is a vowel. Run 2 Enter an alphabet: a a is a vowel. Run 3 Enter an alphabet: h h is a consonant. Run 4 Enter an alphabet: T T is a consonant. Run 5 Enter an alphabet: 5 Error! Non-alphabetic character.