Check whether a character is a vowel or consonant in C
Understanding Vowels and Consonants
A vowel is a letter (a, e, i, o, u) that represents an open speech sound. A consonant is any other letter in the alphabet.
We will explore three different methods to check whether a character is a vowel or consonant in C.
Method 1: Using if-else
This method checks if the character belongs to the set of vowels.
#include <stdio.h> void checkVowelOrConsonant(char ch) { if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' || ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U') { printf("%c is a vowel\n", ch); } else { printf("%c is a consonant\n", ch); } } int main() { char ch; printf("Enter a character: "); scanf(" %c", &ch); checkVowelOrConsonant(ch); return 0; }
Output: a is a vowel
Method 2: Using Switch Case
This method uses a switch statement to determine if the character is a vowel.
#include <stdio.h> void checkVowelOrConsonant(char ch) { switch (ch) { case 'a': case 'e': case 'i': case 'o': case 'u': case 'A': case 'E': case 'I': case 'O': case 'U': printf("%c is a vowel\n", ch); break; default: printf("%c is a consonant\n", ch); } } int main() { char ch; printf("Enter a character: "); scanf(" %c", &ch); checkVowelOrConsonant(ch); return 0; }
Output: b is a consonant
Method 3: Using Recursion
This method checks vowels recursively.
#include <;stdio.h> int isVowel(char ch) { if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' || ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U') { return 1; } return 0; } void checkVowelOrConsonant(char ch) { if (isVowel(ch)) { printf("%c is a vowel\n", ch); } else { printf("%c is a consonant\n", ch); } } int main() { char ch; printf("Enter a character: "); scanf(" %c", &ch); checkVowelOrConsonant(ch); return 0; }
Output: o is a vowel