C++ Program to Vowel or Consonant

In this C++ program, we have to check whether the given character is a vowel or consonant.

How can we check whether the character is 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 ). 

  • If it is true then the entered character is a vowel. 
  • If the character is an alphabet but not a vowel, then it is a consonant.
  • If it is neither vowel nor consonant, then it is not an alphabet.

How can we check for vowels and consonants in C++?

Ask the user to enter a character. Store the character entered by the user into a character type variable c. The entered character maybe

  • Uppercase letter
  • Lowercase letter
  • Not alphabet

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.

  1. First, check whether it is an alphabet or not. For this, Function isalpha can be used. Isalpha()is a function in C++ used to check if the given character is an alphabet or not?
  2. Next, we have to check for upper case vowels. For this uppercase = ( c = ‘A’ // ‘E’ //‘I’ // ‘O’ // ‘U’ ); If this evaluates to 1, then it is a vowel.
  3. we have to check for lower case vowels. For this, lowercase = ( c = ‘a’ // ‘e’ // ‘i’ // ‘o’ // ‘u’ ); If this evaluates to 1, then it is a vowel.
  4. Finally, If the entered character is an alphabet and not a lowercase or uppercase vowel then it is a consonant.

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 }.
 

ALGORITHM

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.

C++ Source Code

                                          #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;
}
                                      

OUTPUT

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.