C++ Program To Find If A Character Is A Vowel Or Consonant - C++ Programming Tutorial
C++ Course / C++ Programs / C++ Program To Find If A Character Is A Vowel Or Consonant

C++ Program To Find If A Character Is A Vowel Or Consonant

BLUF: Mastering C++ Program To Find If A Character Is A Vowel Or Consonant is a critical step in becoming a proficient C++ developer. This lesson provides a deep dive into the syntax, performance considerations, and real-world applications of this concept.
Key Performance Insight: C++ Program To Find If A Character Is A Vowel Or Consonant

C++ is renowned for its efficiency. Learn how C++ Program To Find If A Character Is A Vowel Or Consonant enables low-level control and high-performance computing in the tutorial below.

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:

  • Obtain User Input: Request that the user input a character.
  • Store the Input: Capture the entered Character and store it in a variable (e.g., Character).
  • 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.
  • 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.
  • 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.
  • 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++, multiple techniques exist for identifying whether a specific character is a vowel or a consonant. The three popular approaches include utilizing switch-case statements, if-else statements, and a more straightforward method that employs arrays. Now, let's explore each method through an illustrative example:

Method 1: If-else statements

Example

#include <iostream>
using namespace std;

int main() {
    char character;
    cout << "Enter a character: ";
    cin >> character;
    // Convert the Character to lowercase to handle both uppercase and lowercase letters
    character = tolower(character);

    if (character == 'a' || character == 'e' || character == 'i' || character == 'o' || character == 'u') {
        cout << character << " is a vowel." << endl;
    } else {
        cout << character << " is a consonant." << endl;
    }

    return 0;
}

Output:

Output

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

Explanation:

In this script, we initially change the character to lowercase to account for both uppercase and lowercase inputs. Subsequently, through conditional statements, we check if the character corresponds to any of the vowels ('a', 'e', 'i', 'o', 'u').

Method 2: Using switch-case statements

Example

#include <iostream>
using namespace std;

int main() {
    char character;
    cout << "Enter a character: ";
    cin >> character;

    // Convert the Character to lowercase to handle both uppercase and lowercase letters
    character = tolower(character);

    switch (character) {
        case 'a':
        case 'e':
        case 'i':
        case 'o':
        case 'u':
            cout << character << " is a vowel." << endl;
            break;
        default:
            cout << character << " is a consonant." << endl;
            break;
    }

    return 0;
}

Output:

Output

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

Explanation:

In this script, we additionally change the character to lowercase and subsequently employ a switch-case block to verify if it corresponds to any of the vowels.

Method 3: Using an array

Example

#include <iostream>
#include <cstring>
using namespace std;

int main() {
    char character;
    cout << "Enter a character: ";
    cin >> character;

    // Convert the Character to lowercase to handle both uppercase and lowercase letters
    character = tolower(character);

    char vowels[] = {'a', 'e', 'i', 'o', 'u'};
    bool isVowel = false;

    for (int i = 0; i < strlen(vowels); i++) {
        if (character == vowels[i]) {
            isVowel = true;
            break;
        }
    }

    if (isVowel) {
        cout << character << " is a vowel." << endl;
    } else {
        cout << character << " is a consonant." << endl;
    }

    return 0;
}

Output:

Output

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

Explanation:

In this instance, the vowels are saved in an array after converting the letter to lowercase. We iterate through the array to verify if the Character corresponds to any of the vowels.

While all three approaches achieve the identical outcome, they accomplish this by distinguishing between vowels and consonants in characters through different control mechanisms. Opt for the method that is easiest to understand and most suitable for your particular use case.

By using the Find Function:

Now, the find function, commonly employed with strings and placeholders, is not the primary option for this particular requirement. In comparable scenarios, differentiating between a letter being a vowel or a consonant typically requires a comparative examination.

To ascertain whether a letter belongs to the category of vowels or consonants using the discovery technique, you would scan for it within a string containing vowels. The subsequent C++ code showcases this method.

Code:

Example

#include <iostream>
#include <string>
using namespace std;

int main() {
    char character;
    string vowels = "aeiouAEIOU";

    cout << "Enter a character: ";
    cin >> character;

    // Check if the Character is found in the 'vowels' string
    if (vowels.find(character) != string::npos) {
        cout << character << " is a Vowel." << endl;
    } else {
        cout << character << " is a Consonant." << endl;
    }

    return 0;
}

Output for Various Inputs:

Output

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

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience