There are a total of 26 letters in the English alphabet. There are 5 vowel letters and 21 consonant letters. The 5 vowel letters are: "a", "e", "i", "o", and "u" rest are consonants. There are different approaches to checking whether a character is a vowel or a consonant.
These are the different Approaches to Check Whether a Character is a Vowel or Consonant:
Table of Content
Using Conditional Statements (if-else)
This approach uses a series of if-else statements to check if the given character is a vowel. We first convert the character to lowercase to handle both uppercase and lowercase inputs consistently. Then, we compare the character against each vowel (a, e, i, o, u). If the character matches any of these, it is a vowel otherwise it is consonant.
Example: This example shows the use of an if-else statement to Check Whether a Character is a Vowel or Consonant.
<?php
// Function to check whether a character
// is a vowel or consonant using if-else
function checkVowelOrConsonant($char)
{
// Convert character to lowercase
$charLower = strtolower($char);
// Check if the character is a vowel
if (
$charLower == 'a' || $charLower == 'e'
|| $charLower == 'i' || $charLower == 'o'
|| $charLower == 'u'
) {
echo "$char is a vowel" . PHP_EOL;
} else {
echo "$char is a consonant" . PHP_EOL;
}
}
checkVowelOrConsonant('t');
checkVowelOrConsonant('E');
?>
Output
e is a vowel E is a vowel
Using the in_array() Function
In this method, we utilize PHP's in_array function, which checks if a value exists in an array. We create an array containing all the vowels. Then, we use in_array to check if the character is in the array of vowels. If it is, we identify it as a vowel, otherwise, it's a consonant.
Example: This example shows the use of in_array() function to Check Whether a Character is a Vowel or Consonant.
<?php
// Function to check whether a character is
// a vowel or consonant using in_array function
function checkVowelOrConsonant($char)
{
// Convert character to lowercase
$charLower = strtolower($char);
// Array of vowels
$vowels = array('a', 'e', 'i', 'o', 'u');
// Check if the character is in the vowels array
if (in_array($charLower, $vowels)) {
echo "$char is a vowel" . PHP_EOL;
} else {
echo "$char is a consonant" . PHP_EOL;
}
}
checkVowelOrConsonant('e');
checkVowelOrConsonant('E');
?>
Output
e is a vowel E is a vowel
Using a Switch Statement
This approach uses a switch statement to evaluate the character. We first convert the character to lowercase. The switch statement then checks if the character matches any of the cases for vowels (a, e, i, o, u). If a match is found, it is labeled as a vowel, otherwise, the default case classifies it as a consonant.
Example: This example shows the use of switch statement to Check Whether a Character is a Vowel or Consonant.
<?php
// Function to check whether a character
// is a vowel or consonant using switch statement
function checkVowelOrConsonant($char)
{
// Convert character to lowercase
$charLower = strtolower($char);
// Switch statement to check the character
switch ($charLower) {
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
echo "$char is a vowel" . PHP_EOL;
break;
default:
echo "$char is a consonant" . PHP_EOL;
break;
}
}
checkVowelOrConsonant('e');
checkVowelOrConsonant('E');
?>
Output
e is a vowel E is a vowel