C++ Program to find if a character is a Vowel or Consonant

In this article, we will discuss how to find a character is a vowel or constant in C++. If we want to check whether a letter is either a vowel or a consonant, we can use the program written below:

  1. Obtain User Input: Request that the user input a character.
  2. Store the Input: Capture the entered Character and store it in a variable (e.g., Character).
  3. Check if the Character is a Vowel:
    • Check if the Character is equal to 'a', 'e', 'i', 'o', or 'u' (all lowercase) using an if statement.
    • If it matches any of these characters, consider it a vowel.
    • You can check the uppercase versions of these letters ('A', 'E', 'I', 'O', 'U') separately if needed.
  4. Display the Result:
    • If the Character is a vowel, print a message indicating it is a vowel.
    • If the Character is not a vowel (i.e., it does not match any specified characters), assume it is a consonant and print a message indicating it's a consonant.
    • If the input letter is one of the vowels in the given set, this function detects it as a vowel. It is considered a consonant otherwise.

In C++, there are several methods to determine if a given letter is a vowel or a consonant. The three common methods are switch-case statements, if-else statements, and a simpler method that makes use of arrays. Let us discuss each with an example:

Method 1: If-else statements

Output:

Enter a character: a
a is a vowel.
Enter a character: B
b is a consonant.

Explanation:

In this code, we first convert the Character to lowercase to handle both uppercase & lowercase input. After that, using if-else expressions, we determine whether the Character is one of the vowels ('a', 'e', 'i', 'o', 'u').

Method 2: Using switch-case statements

Output:

Enter a character: e
e is a vowel.
Enter a character: X
x is a consonant.

Explanation:

In this code, we also convert the Character to lowercase and then use a switch-case statement to check if it matches any of the vowels.

Method 3: Using an array

Output:

Enter a character: i
i is a vowel.
Enter a character: Y
y is a consonant.

Explanation:

In this example, the vowels are stored in an array after this code initially makes the letter lowercase. We cycle over the array to check if the Character matches any of the vowels.

While all three methods reach the same result, they do so by identifying vowels and consonants in letters using various control structures. Choose the one that is the simplest to comprehend and best matches your specific application.

By using the Find() Function:

Now, the find() method, generally used with strings and holders, is not the immediate choice for this task. In similar cases, distinguishing whether a letter is a vowel or a consonant generally involves relative analysis.

To determine if a letter falls into the order of vowels or consonants using the discovery() system, you'd search for it within a string of vowels. The following C++ program demonstrates this approach

Code:

Output for Various Inputs:

Input: 'a'
Output: "A is a Vowel."
Input: 'B'
Output: "B is a Consonant.





Latest Courses