C Program to Check Whether a Character is Vowel or Consonant

In this example, you will learn to check whether an alphabet entered by the user is a vowel or a consonant.
vowels and consonants in English alphabet

To understand this example, you should have the knowledge of the following C programming topics:

The five letters A, E, I, O and U are called vowels. All other alphabets except these 5 vowels are called consonants.

This program assumes that the user will always enter an alphabet character.


Program to Check Vowel or consonant

#include <stdio.h>
int main() {
    char c;
    int lowercase, uppercase;
    printf("Enter an alphabet: ");
    scanf("%c", &c);

    // evaluates to 1 if variable c is lowercase
    lowercase = (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u');

    // evaluates to 1 if variable c is uppercase
    uppercase = (c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U');

    // evaluates to 1 if c is either lowercase or uppercase
    if (lowercase || uppercase)
        printf("%c is a vowel.", c);
    else
        printf("%c is a consonant.", c);
    return 0;
}

Output

Enter an alphabet: G
G is a consonant.

The character entered by the user is stored in variable c.

The lowercase variable evaluates to 1 (true) if c is a lowercase vowel and 0 (false) for any other characters.

Similarly, the uppercase variable evaluates to 1 (true) if c is an uppercase vowel and 0 (false) for any other character.

If either lowercase or uppercase variable is 1 (true), the entered character is a vowel.

However, if both lowercase and uppercase variables are 0, the entered character is a consonant.