There are five vowel letters in English: a, e, i, o, and u. All other alphabetic letters are consonants.
- The same vowels apply to uppercase letters: A, E, I, O, and U.
- This program checks whether a given character is a vowel or consonant.
-768.png)
Algorithm
The basic algorithm is:
- Take a character as input.
- Check whether the character is one of a, e, i, o, u or their uppercase forms.
- If the character matches any vowel, print Vowel.
- Otherwise, print Consonant.
1. Check Vowel or Consonant Using if-else in C
We can use the logical OR (||) operator to check the character against all five vowels in both lowercase and uppercase.
#include <stdio.h>
// Driver code
int main()
{
char ch = 'A';
// Checking if the character ch
// is a vowel or not.
if (ch == 'a' || ch == 'A' || ch == 'e' || ch == 'E'
|| ch == 'i' || ch == 'I' || ch == 'o' || ch == 'O'
|| ch == 'u' || ch == 'U') {
printf("The character %c is a vowel.\n", ch);
}
else {
printf("The character %c is a consonant.\n", ch);
}
return 0;
}
Output
The character A is a vowel.
Explanation: The if condition compares the character with all five vowels in both uppercase and lowercase. If any comparison is true, the character is a vowel; otherwise, it is considered a consonant.
2. Check Vowel or Consonant Using strchr() in C
The strchr() function is used to search for the entered character in the vowels array. If the character is found, the isVowel function returns 1, otherwise, it returns 0.
#include <stdio.h>
#include <string.h>
int isVowel(char ch)
{
// Make the list of vowels
char vowels[] = "aeiouAEIOU";
return (strchr(vowels, ch) != NULL);
}
// Driver Code
int main()
{
if (isVowel('a'))
printf("a is vowel\n");
else
printf("a is consonant\n");
return 0;
}
Output
a is vowel
Explanation: The strchr() function searches for ch in the string "aeiouAEIOU". If the character is found, strchr() returns a non-NULL pointer, indicating that the character is a vowel.