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.

Advantages of the Vowel or Consonant Program in C

  • Introduction to Conditional Statements : This program introduces beginners to conditional statements in C programming, specifically the if-else statement. It demonstrates how to use conditions to make decisions based on the input character.

  • Understanding Character Manipulation : Writing this program helps you understand how to manipulate characters in C programming. You will learn how to compare characters and determine their properties based on their ASCII values.

  • Building Block for Text Processing : The Vowel or Consonant Program serves as a building block for programs that involve text processing and character analysis. It lays the foundation for understanding and implementing algorithms that deal with textual data

Importance of Practicing the Vowel or Consonant Program for Beginners

  • Understanding of Character Properties : Implementing this program enhances your understanding of character properties in C programming. You will learn about vowels and consonants and how to differentiate between them based on their characteristics.

  • Preparation for String Processing : The skills and concepts you learn from this program prepare you for string processing tasks that involve analyzing and manipulating characters in strings. It equips you with the foundational knowledge needed to work with textual data in C programming.

  • Development of Logical Thinking : This program helps beginners develop logical thinking by challenging them to analyze the properties of characters and make decisions based on predefined rules. It improves your ability to think critically and solve problems effectively.

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.
#include <stdio.h>

int main(){
    char c;
    printf("Enter a Character: ");
    scanf("%c", &c);
    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);
    }
    return 0;
}
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.

#include <stdio.h>

#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);

    if(isVowel(c)){
        printf("%c is a Vowel\n", c);
    } else {
        printf("%c is a Consonant\n", c);
    }
  
    return 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;
}

int isLowerCase(char c){
    if(c >= 'a' && c<= 'z')
        return 1;
    else 
        return 0;    
}
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

#include <stdio.h>
 
int main(){
    char c;
    printf("Enter a Character: ");
    scanf("%c", &c);
    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);
    }    
    return 0;
}
Output
Enter a Character: A
A is a Vowel

Important Points to Remember

  • Character Encoding : Be aware of the character encoding used in C programming (e.g., ASCII) and how it represents characters numerically. This knowledge is essential for comparing and manipulating characters effectively.

  • Error Handling : Implement error-handling mechanisms to handle unexpected inputs gracefully. Display informative messages to guide the user on how to correct the input if it is invalid.

  • Consider Localization : If your program needs to support multiple languages, consider the specific rules for vowels and consonants in those languages. Ensure that your program is flexible enough to accommodate different language requirements.

Related Topics
C program to check year is leap year or not
C program to check odd or even numbers
C program to check a number is palindrome or not
C program to check armstrong number
C Program to find frequency of characters in a string
C program to add two complex numbers
C Program to calculate factorial of a number
List of all C programs