![]() |
C Programming Vowel Checker |
Hello guys, today I am gonna show you hot to check if the character input by user is either vowel or consonant. I know it's a very simple and probably most easy problem but still this can be little hard for others who are completely new to C Programming.
Here I have shown Three methods by which you can check the input character. First one is by using if condition which is most easy, second one is using switch statement which is also easy and the last one is using our own function which is considerably more complicated. So lets see the three codes.
Using If Statement
#include<stdio.h> #include<conio.h int main() { char ch; printf("Enter a character\n"); scanf("%c", &ch); if (ch == 'a' || ch == 'A' || ch == 'e' || ch == 'E' || ch == 'i' || ch == 'I' || ch =='o' || ch=='O' || ch == 'u' || ch == 'U') printf("%c is a vowel.\n", ch); else printf("%c is not a vowel.\n", ch); getch(); return 0; }
Using Switch Statement
#include <stdio.h> #include<conio.h> int main() { char ch; printf("Input a character\n"); scanf("%c", &ch); switch(ch) { case 'a': case 'A': case 'e': case 'E': case 'i': case 'I': case 'o': case 'O': case 'u': case 'U': printf("%c is a vowel.\n", ch); break; default: printf("%c is not a vowel.\n", ch); } getch(); return 0; }
Our Own Vowel Checker Function
int check_vowel(char a) { if (a >= 'A' && a <= 'Z') a = a + 'a' - 'A'; /* Converting to lower case or use a = a + 32 */ if (a == 'a' || a == 'e' || a == 'i' || a == 'o' || a == 'u') return 1; return 0; }
Word of Caution: You can also make similar Function to check whether the character is consonant or not. But keep in mind that you always do the range checking otherwise your program will not be fool proof against special character as input.
:) nic
ReplyDelete