C Program To Check Whether a Character is Vowel or Consonant

In this post, you will get to know how to check whether a character entered by the user is a vowel or consonant.

C Program To Check Whether a Character is Vowel or Consonant

This program takes a character value as input from the user. Then it checks whether the character entered is vowel or consonant using if. . . else statement.

This program checks for both lowercase and uppercase vowels and consonants.

The Five letters A, E, I, O and U are called Vowels. All remaining 21 characters are known as Consonants.

So, Without further ado, let’s begin the tutorial.

C Program To Check Whether a Character is Vowel or Consonant

// C Program To Check Whether a Character is Vowel or Consonant
#include <stdio.h>
int main(){
    char ch;
    int lowercase, uppercase;
    
    // Asking for Input
    printf("Enter the Character: ");
    scanf("%c", &ch);
    
    // logic
    lowercase = (ch == "a" || ch == "e" || ch == "i" || ch == "o" || ch == "u");
    
    uppercase = (ch == "A" || ch == "E" || ch == "I" || ch == "O" || ch == "U");
    
    // Displaying Output
    if (lowercase || uppercase)
        printf("%c is a vowel.", ch);
    else
        printf("%c is a consonant.", ch);
        
    return 0;    
}

Output

Enter the Character: K
K is a consonant.

How Does This Program Work ?

    char ch;
    int lowercase, uppercase;

In this program we have declared a string type variable named as ch. And at the same time we have also declared two int variable names as lowercase and uppercase.

    // Asking for Input
    printf("Enter the Character: ");
    scanf("%c", &ch);

Then, the user is asked to enter any character. The character entered by the user is stored in variable ch.

// logic
    lowercase = (ch == "a" || ch == "e" || ch == "i" || ch == "o" || ch == "u");
    
    uppercase = (ch == "A" || ch == "E" || ch == "I" || ch == "O" || ch == "U");

lowercase variable will evaluate True if ch is a lowercase vowel and False for any other character.

Similarly, the uppercase variable will evaluate True if ch is an uppercase vowel and False for any other character.

// Displaying Output
    if (lowercase || uppercase)
        printf("%c is a vowel.", ch);
    else
        printf("%c is a consonant.", ch);

If either of the lowercase or uppercase variables is True, then the entered character is a vowel.

And if both lowercase and uppercase variables are False, then the entered character is a consonant.

Some used terms are as follow:

#include <stdio.h> – In the first line we have used #include, it is a preprocessor command that tells the compiler to include the contents of the stdio.h(standard input and output) file in the program.

The stdio.h is a file which contains input and output functions like scanf() and printf() to take input and display output respectively. 

Int main() – Here main() is the function name and int is the return type of this function. The Execution of any Programming written in C language begins with main() function. 

scanf() – scanf() function is used to take input from the user. 

printf() – printf() function is used to display and print the string under the quotation to the screen.

|| – It is known as Logical OR Operator. If any of the two operations is non-zero, then the condition becomes true.

If. . . else – An if statement can be followed by an optional else statement, which executes when the Boolean expression is false. If Statement executes when the Boolean expression is True.

// – Used for Commenting in C.

If the Character is not an Alphabet

The above program will read any character other than vowel as consonant which is not right. For example, if the user entered @ symbol then this program will show a consonant.

To solve this issue we can use the isalpha() function. 

// C Program To Check Whether a Character is Vowel or Character
#include <stdio.h>
#include <ctype.h>
int main(){
    char ch;
    int lowercase, uppercase;
    
    // Asking for Input
    printf("Enter an Alphabet: ");
    scanf("%c", &ch);
    
    // logic
    lowercase = (ch == "a" || ch == "e" || ch == "i" || ch == "o" || ch == "u");
    uppercase = (ch == "A" || ch == "E" || ch == "I" || ch == "O" || ch == "U");
    
    // Displaying Output 
    if(!isalpha(ch))
        printf("%c is neither vowel nor consonant.", ch);
    else if (lowercase || uppercase)
        printf("%c is a vowel.", ch);
    else
        printf("%c is a consonant.", ch);
        
    return 0;    
}

Output

Enter an Alphabet: @
@ is neither vowel nor consonant.
    // Displaying Output 
    if(!isalpha(ch))
        printf("%c is neither vowel nor consonant.", ch);
    else if (lowercase || uppercase)
        printf("%c is a vowel.", ch);
    else
        printf("%c is a consonant.", ch);

isalpha() is a function in C which can be used to check if the passed character is an alphabet or not.

Conclusion

I hope after going through this tutorial, you understand how to check whether a character is a vowel or consonant.

If you have any query regarding the topic, feel free to contact us in the Comment Section. We will be delighted to help you.

Leave a Comment

Your email address will not be published. Required fields are marked *