In the English alphabet, the vowels are a, e, i, o, and u, while the remaining alphabetic characters are consonants. A character can be checked against these vowels to determine whether it is a vowel or consonant.
- Both uppercase and lowercase vowels can be checked.
- The character can be classified using conditional statements or a string search.
-768.png)
Approach 1: Using if-else Statement
The character is compared with each vowel using the logical OR (||) operator. If it matches any vowel, it is classified as a vowel; otherwise, it is classified as a 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
Approach 2: Using find()
The std::string::find() function can be used to search for the given character in a string containing all vowels. If the character is found, it is a vowel; otherwise, it is a consonant.
#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