C Program to Check Whether an Alphabet is Vowel or Consonant

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

A vowel is the alphabets that represents a speech sound created by the relatively free passage of breath through the larynx and oral cavity. Letters that are not vowels are consonants. English has five proper vowel letters (A, E, I, O, U) all alphabets except these characters are consonants.

C Program to check whether an input character is vowel or consonant

In the following program, we take a character as input from user and store in variable c. Then, we check whether it is any one of these ten characters(lower and upper case vowels) a, A, e, E, i, I, o, O, u and U using || operator. If input character is any one of these ten vowel characters, then it is a vowel otherwise a consonant.

C Program to check whether an input character is vowel or consonant 1

/*
* C Program to check whether an alphabet is vowel or Consonant
* Vowels: {A,E,I,O,U}
*/
#include <stdio.h>
#include <conio.h>
 
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(c == 'a' || c == 'e' || c =='i' || c=='o' || c=='u' || c=='A'
          || c=='E' || c=='I' || c=='O' || c=='U'){
        printf("%c is a Vowel\n", c);
    } else {
        printf("%c is a Consonant\n", c);
    }
    getch();
    return 0;
}

Program Output

Enter a Character: A
A is a Vowel
Enter a Character: D
D is a Consonant

C program to check whether a character is vowel or consonant using function

In this program, we first check whether input alphabet is lower case or not using “isLowerCase” function which returns 1 if passed character is lower case character. Then we convert lower case characters to their corresponding upper case character by subtracting 32.

Difference between the ASCII value of any lower case character and it’s corresponding upper case character is 32 (‘A’ + 32 = ‘a’). After conversion of lower to upper case char, it checks whether it is vowel or consonant using isVowel function.

C program to check whether a character is vowel or consonant using function 1

/*
* C Program to check whether an alphabet is vowel or 
* consonant using function
* Vowels: {A,E,I,O,U}
*/
#include <stdio.h>
#include <conio.h>
 
/* 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;    
}

Program Output

Enter a Character: e
e is a Vowel
Enter a Character: f
f is a Consonant

C Program to check whether an alphabet is vowel or consonant using switch statement

To understand this program, you should have knowledge of Switch Statements

C Program to check whether an alphabet is vowel or consonant using switch statement 1

/*
* C Program to check whether a alphabet is vowel or not
* using switch statement
* Vowels: {A,E,I,O,U}
*/
#include <stdio.h>
#include <conio.h>
  
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}
       using switch statement */
    switch(c)
    {
        case 'A':
        case 'E':
        case 'I':
        case 'O':
        case 'U':
        case 'a':
        case 'e':
        case 'i':
        case 'o':
        case 'u':
            printf("%c is a Vowel\n", c);
            break;
        default:
            printf("%c is a Consonant\n", c);
    }    
    getch();
    return 0;
}

Program Output

Enter a Character: A
A is a Vowel