C program to check whether a character is a vowel or consonant (Including alphabets and non-alphabetic characters)

by | Jan 10, 2023 | C, C Programs

Introduction

This program will check whether the character entered by the user is a vowel or a consonant same as the previous one. The difference is that in the previous program we were assuming that the user will enter an alphabet only.

But what if the character entered by the user is not an alphabet?

To tackle this problem, we will use a predefined function of the C language i.e., isalpha() function.

To make this function available, we need to include ‘ctype’ library in our code.

To understand this example, you should know about

1. C programming operators

2. if…else statement

3. Predefined functions(such as isalpha() function)

Vowels are a, e, i, o, u, A, E, I, O, and U.

All other alphabets, including lower case and upper case, except vowels are consonants.

Program

#include<stdio.h>
#include<ctype.h> // This library will make isalpha function available
int main()
{
char schar;
printf("Enter a character:");
scanf("%c",&schar);


if(!isalpha(schar)) //schar is passed in the function to check its behavior
{
printf("Entered character is not a alphabet");
}
else if ( (schar=='a' || schar=='e'|| schar=='i'|| schar=='o'|| schar=='u') ||
(schar=='A' || schar=='E'|| schar=='I'|| schar=='O'|| schar=='U') ) // '||'(or) operator is used
{
printf("Character %c is a vowel",schar);
}
else
{
printf("Character %c is a consonent",schar);
}
return 0;
}

 

Output

Explanation

The character value entered by the user is ‘’.

if(!isalpha(ch))

When ‘ch’ is passed in the above function it will give true as a result because ‘’ is not an alphabet; thus the block of ‘if’ statement gets executed.

 

0 Comments

Submit a Comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.