1. Introduction
A fundamental exercise in C programming for beginners involves understanding conditional statements through the classification of alphabets. One such common problem is to determine whether a given character is a vowel or a consonant. In this guide, we will learn how to write a C program to check whether a given character is Vowel or Consonant.
2. Program Overview
1. Prompt the user to input a character.
2. Convert the character to lowercase to ensure the check is case insensitive.
3. Use a switch-case statement to determine if the character is a vowel (i.e., 'a', 'e', 'i', 'o', or 'u').
4. If it's a vowel, print the result; if it's not a vowel and is an alphabet, it's a consonant. If it's neither a vowel nor a consonant (not an alphabet), inform the user.
3. Code Program
#include <stdio.h>
int main() {
char c;
int isLowercaseVowel, isUppercaseVowel;
// Asking user to input a character
printf("Enter a character: ");
scanf("%c", &c);
// Check if it's one of the lowercase vowels
isLowercaseVowel = (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u');
// Check if it's one of the uppercase vowels
isUppercaseVowel = (c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U');
// Determine the type of the character
if (isLowercaseVowel || isUppercaseVowel)
printf("%c is a vowel.", c);
else if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) // Check if it's an alphabet character
printf("%c is a consonant.", c);
else
printf("%c is not an alphabet character.", c);
return 0;
}
Output:
Enter a character: e e is a vowel. Enter a character: D D is a consonant. Enter a character: 1 1 is not an alphabet character.
4. Step By Step Explanation
1. The user is prompted to input a character.
2. Two integer variables, isLowercaseVowel and isUppercaseVowel, are used to store the result of checking whether the character is a lowercase or uppercase vowel, respectively.
3. If the character is either a lowercase or uppercase vowel, it is identified as a vowel.
4. If it's not a vowel but lies within the alphabetic range (either lowercase or uppercase), it's identified as a consonant.
5. Otherwise, the character is neither a vowel nor a consonant, so it's identified as not an alphabet character.
Comments
Post a Comment
Leave Comment