Check Whether a Character is Vowel or Consonant in C++



In this article, we'll show you how to write a C++ program to check if a given character is a vowel or a consonant. Vowels are the letters 'a', 'e', 'i', 'o', 'u'(both uppercase and lowercase), and all other alphabetic characters are consonants.

For example, if we input the character 'a', the program will output 'vowel'. If we input 'b', it will output 'consonant'.

In C++, there are different ways to check if a character is a vowel or consonant. Below are the common methods:

Check if Character is Vowel or Consonant Using if-else Statement

In this approach, we use a simple if-else statement to check if the character matches any vowel. Based on the match, we decide whether it's a vowel or a consonant.

Example

In this example, we use if-else conditions to check whether the given character is a vowel (both lowercase and uppercase). If it is, we display "Vowel" otherwise, we display "Consonant."

#include <iostream>
using namespace std;

int main() {
    char ch = 'e'; // Predefined character

    if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' || 
        ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U') {
        cout << ch << " is a Vowel" << endl;
    } else {
        cout << ch << " is a Consonant" << endl;
    }

    return 0;
}

The output of the above program displays whether the given character is a vowel or consonant.

e is a Vowel

Time Complexity: O(1).

Space Complexity: O(1).

Check if Character is Vowel or Consonant Using switch-case Statement

In this approach, we use the switch-case statement to check whether a character is a vowel or consonant. It's similar to the if-else method but provides a more structured way of handling multiple conditions.

Example

Here's a complete C++ program to check if a character is a vowel or consonant using a switch-case statement:

#include <iostream>
using namespace std;

int main() {
    char ch = 'I'; // Predefined character
    
    switch(ch) {
        case 'a': case 'e': case 'i': case 'o': case 'u':
        case 'A': case 'E': case 'I': case 'O': case 'U':
            cout << ch << " is a Vowel" << endl;
            break;
        default:
            cout << ch << " is a Consonant" << endl;
    }
    
    return 0;
}

The output displays whether the given character is a vowel or consonant:

I is a Vowel

Time Complexity: O(1).

Space Complexity: O(1).

Check if Character is Vowel or Consonant Using User-defined Function

In this approach, we put the logic inside a function that checks if a character is a vowel or consonant. This makes the code easier to reuse and organize.

Example

In this example, we create a function named checkVowelConsonant() that takes a character as an argument and determines whether it is a vowel or consonant. The function then prints the result accordingly.

#include <iostream>
using namespace std;

void checkVowelConsonant(char ch) {
    if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' ||
        ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U') {
        cout << ch << " is a Vowel" << endl;
    } else {
        cout << ch << " is a Consonant" << endl;
    }
}

int main() {
    char ch = 'O'; // Predefined character
    checkVowelConsonant(ch);
    
    return 0;
}

The output below shows that the character O is a vowel:

O is a Vowel 

Time Complexity: O(1).

Space Complexity: O(1)..

Updated on: 2025-05-09T15:16:30+05:30

12K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements