Open In App

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

Last Updated : 12 Sep, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

In English Alphabet, vowels are ‘a’, ‘e’, ‘i’, ‘o’, and ‘u’. The rest of the remaining characters (like ‘b’, ‘c’, ‘d’, ‘f’ ….) are consonants. In this article, we will learn to write a C++ program to check whether a character is Vowel or Consonant.

check vowel or cosonant character in c++

C++ Program to Check Whether a Character is a Vowel or Consonant

In this method, we check whether the given character matches any of the 5 vowels using if-else statement. If yes, we print “Vowel”, else we print “Consonant”.

C++




// C++ program to check if a given
// character is vowel or consonant.
#include <iostream>
using namespace std;
 
// Function to check whether a
// character is vowel or not
void vowelOrConsonant(char x)
{
    if (x == 'a' || x == 'e' || x == 'i' || x == 'o'
        || x == 'u' || x == 'A' || x == 'E' || x == 'I'
        || x == 'O' || x == 'U')
        cout << "Vowel" << endl;
    else
        cout << "Consonant" << endl;
}
 
// Driver code
int main()
{
    vowelOrConsonant('c');
    vowelOrConsonant('E');
    return 0;
}


Output

Consonant
Vowel

Complexity Analysis

  • Time Complexity: O(1)
  • Auxiliary Space: O(1)

Check Vowel or Consonant using find() Function

In the below C++ program, the find() function is used to search for the character in the string containing vowel letters. If the character is found, the isVowel function return 1, else it returns 0.

C++




// C++ program to check vowel or consonant using find()
// function
#include <iostream>
#include <string>
 
using namespace std;
 
int isVowel(char ch)
{
    // Make the list of vowels
    string str = "aeiouAEIOU";
    return (str.find(ch) != string::npos);
}
 
// Driver code
int main()
{
    if (isVowel('a'))
        cout << "a is vowel" << endl;
    else
        cout << "a is consonant" << endl;
 
    return 0;
}


Output

a is vowel

Complexity Analysis

  • Time Complexity: O(1)
  • Auxiliary Space: O(1)

Refer to the complete article Program to find if a character is vowel or Consonant for more methods to check if a character is a vowel or consonant.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads