C Example for Beginners: C Program to Check Whether a Character is a Vowel or Consonant

Hits: 22

(C programming Example for Beginners)

C Program to Check Whether a Character is a Vowel or Consonant

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

The five letters AEIO 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_vowel, uppercase_vowel;
    printf("Enter an alphabet: ");
    scanf("%c", &c);

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

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

    // evaluates to 1 (true) if c is a vowel
    if (lowercase_vowel || uppercase_vowel)
        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_vowel variable evaluates to 1 (true) if c is a lowercase vowel and 0 (false) for any other characters.

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

If either lowercase_vowel or uppercase_vowel variable is 1 (true), the entered character is a vowel. However, if both lowercase_vowel and uppercase_vowel variables are 0, the entered character is a consonant.

Note: This program assumes that the user will enter an alphabet. If the user enters a non-alphabetic character, it displays the character is a constant.

To fix this, we can use the isalpha() function. The islapha() function checks whether a character is an alphabet or not.


#include <ctype.h>
#include <stdio.h>

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

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

   // evaluates to 1 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! Non-alphabetic character.");
   else if (lowercase_vowel || uppercase_vowel)
      printf("%c is a vowel.", c);
   else
      printf("%c is a consonant.", c);

   return 0;
}

Now, if the user enters a non-alphabetic character, you will see:

Enter an alphabet: 3
Error! Non-alphabetic character.

 

C Example for Beginners: C Program to Check Whether a Character is a Vowel or Consonant

Sign up to get end-to-end “Learn By Coding” example.



Disclaimer: The information and code presented within this recipe/tutorial is only for educational and coaching purposes for beginners and developers. Anyone can practice and apply the recipe/tutorial presented here, but the reader is taking full responsibility for his/her actions. The author (content curator) of this recipe (code / program) has made every effort to ensure the accuracy of the information was correct at time of publication. The author (content curator) does not assume and hereby disclaims any liability to any party for any loss, damage, or disruption caused by errors or omissions, whether such errors or omissions result from accident, negligence, or any other cause. The information presented here could also be found in public knowledge domains.