C Program to Find a Vowel – C programming can be fun if you know the right scripts to use. While it ends up being a high-end programming language, its ability to be flexible and giving its users maximum controls over the variables, is something no programmer can deny. One such program, which involves understanding if an alphabet is a vowel or a consonant, can be written in various formed. Vowels are generally A, E, I, O, and U, while the remaining alphabets are known as consonants. There can be various ways through which it can be checked, such as by using a random program, or by using switches and functions.
If a random program is being used, it can be done through the use of variables and operators. Values can be stored in a variable that can be named C. While many types of vowels, both upper and lower cases can be used and be identified using || operators. The program can tell if the alphabet is uppercase or lowercase, and if it is a vowel.
/*
* C Program to check whether an alphabet is vowel or
* consonant using function
* Vowels: {A,E,I,O,U}
*/
#include
#include
/* Difference between a lowerCase and it's corresponding
upperCase alphabet is 32 -- 'a' - 'A' = 32 */
#define LOWERCASE_TO_UPPERCASE_DIFFERENCE 32
int isVowel(char c);
int isLowerCase(char c);
int main(){
char c;
printf("Enter a Character: ");
scanf("%c", &c);
/* Check if input alphabet is member of set{A,E,I,O,U,a,e,i,o,u} */
if(isVowel(c)){
printf("%c is a Vowel\n", c);
} else {
printf("%c is a Consonant\n", c);
}
getch();
return 0;
}
/*
* Fuction to check whether an alphabet is vowel or not
* returns 1 if passed character is Vowel otherwise 0
*/
int isVowel(char c){
if(isLowerCase(c))
c = c - LOWERCASE_TO_UPPERCASE_DIFFERENCE;
if (c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U')
return 1;
else
return 0;
}
/*
* Function to check whether an alphabet is lowerCase or not
* returns 1 for lower case characters otherwise 0
*/
int isLowerCase(char c){
if(c >= 'a' && c<= 'z')
return 1;
else
return 0;
Result
After using the above mentioned the script, this C Program can tell if the alphabets being entered are vowels or not. If the value is true, which is often denoted by 1 at the compiler, the program will tell that the said alphabet is a vowel, else it will tell if the alphabet is a consonant.
Enter a Character: u
u is a Vowel
Enter a Character: M
M is a Consonant
Conclusion
Apart from this program, there can be functions and switches, which can be used as well to help the compiler understand if the alphabet or character is whether a vowel or a consonant. Switches require switch statements to operate.