Write a C program to check whether an alphabet is vowel or consonant
Description: Take an alphabet as input. Print if it is a vowel or a consonant.
Hint:
- The vowels are a, e, i, o, u (in small and capital letters). Convert the letter to lower case with
tolower()to keep the code short.
C program to check vowel or consonant
#include <stdio.h>
#include <ctype.h>
int main(void) {
char ch;
printf("Enter an alphabet: ");
scanf(" %c", &ch);
switch (tolower(ch)) {
case 'a': case 'e': case 'i': case 'o': case 'u':
printf("%c is a vowel\n", ch);
break;
default:
if (isalpha(ch)) {
printf("%c is a consonant\n", ch);
} else {
printf("%c is not an alphabet\n", ch);
}
}
return 0;
}
Output:
Output of the above program is
Enter an alphabet: E
E is a vowel
How it works
tolower('E')gives'e', which matches a vowel case.- The space before
%cinscanf(" %c")skips the white spaces, like the Enter key. isalpha()checks that the input is a letter.
Read C Switch Statement.
Learn more about the similar topics:
| Tutorials |
|---|
| C If Else Statement |
| C Else If Ladder and Nested If |
| C Switch Statement |
| C While Loop |
| C Do While Loop |
| C For Loop |
| C Nested Loops |