Check whether a Character is a Vowel or Consonant in C

Exercise:

Write a C Program to check whether a character is a vowel or consonant.

Click Here to View the Solution!
#include <ctype.h>
#include <stdio.h>

int main() {
    char c;
    int lowercase_vowel, uppercase_vowel;
    printf("Enter an alphabet: ");
    scanf("%c", &c);

    //Checks if variable c is a lowercase vowel
    lowercase_vowel = (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u');

    //Checks if variable c is a uppercase vowel
    uppercase_vowel = (c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U');

    // Show error message if c is not an alphabet
    if (!isalpha(c)) {
        printf("Error! Entered Character is Non-alphabetic.");
    }
    else if (lowercase_vowel || uppercase_vowel) {
        printf("%c is a vowel.", c);
    }

    else {
        printf("%c is a consonant.", c);
    }
    return 0;
}
Click Here to View the Output!
Enter an alphabet: e
e is a vowel.
Click Here to View the Explanation!
  • This program is used to determine whether the entered character is a vowel or a consonant.
  • In main(), the program requests the user to enter an alphabet and store in the character variable c using the scanf() function.
  • Two integer type variables lowercase_vowels and uppercase_vowels are initialized.
  • The lowercase_vowel returns true if the entered character is a lowercase vowel. Similarly, the uppercase_vowel returns true if c is a upper case vowel.
  • In an if...else statement, a function isalpha() is used that initially checks that whether c is an alphabet. It then checks if c is either a lowercase or an uppercase vowel. If it is neither, the control goes to the else statement that prints that the c is a consonant.
  • The return 0 statement is used to exit the program execution.
%d bloggers like this: