Check whether the alphabet is Vowel or Consonant in C language

Spread the love
program-to-check-vowel-or-consonant-in-c-language

Program Description:

Let’s write a program to check whether the given alphabet is vowel or consonant in C language. We are going to use the if else statement with the comparison operators to check the alphabet

The program should display an error if a value other than the alphabet is entered by the user.

Example Input and Output:

Example 1: Input:

Enter a Character: U

Output:

Given Character U is Vowel

Example 2: Input:

Enter a Character: l

Output:

Given Character l is Consonant

Example 3: Input:

Enter a Character: 10

Output:

Invalid Input : Please enter a Valid alphabet

Pre-Requisites:

We need to have a basic understanding of the if-else statements and the logical operators ( Logical OR and Logical AND)

Check Vowel or Consonant in C Program Explanation:

  • We will ask the user to enter a character and store the value in variable ch
  • Then we use the if else statement with Logical OR to check whether the ch is Vowel or consonant.
  • As we know, if the given character ( ch) is any one of the a , e, i, o, u, Including capital A, E, I, O, U then the character ( ch) is considered as the Vowel.
  • We are checking if the ch is equal to any of the above vowel characters using the following logic (ch == 'a' || ch == 'A' || ch == 'e' || ch == 'E' || ch == 'i' || ch == 'I' || ch == 'o' || ch == 'O' || ch == 'u' || ch == 'U') – which is simply comparing the ch with all aeiou characters one by one.
  • If above check returns True, Then the character ch is Vowel.
  • Otherwise, The given character ch is not Vowel. but we need to check if it valid alphabet.
  • To check alphabet, We use the following logic ( (ch >= ‘a’ && ch <= ‘z’) || (ch >= ‘A’ && ch <= ‘Z’)) – We are checking if the ch is in between the a and z ( capital and lower case alphabets)
  • If the above condition is True, Then the given character ch is Consonant.
  • If the above two conditions are False, Then the character ch is not an Alphabet. It might be number or special character, etc. So display the error message and let the user know about the Invalid Input.

Method 1: Program to check Vowel or Consonant in C Language:

Program Output:

Method 2: Program to check vowel or consonant in C Programming:

We are going to modify above program, Instead of manually checking the given number is alphabet ( using – ( (ch >= ‘a’ && ch <= ‘z’) || (ch >= ‘A’ && ch <= ‘Z’)) ), We can use the Inbuilt function isalpha() from the ctype.h headerfile.

Syntax of isalpha function:

The isalpha() function is available in the ctype.h library and isalpha() function takes a character as input and returns ‘ True if it is an Alphabet

Let’s change the above program and re-write using the isalpha() function.

Program Output:

Method 3: Program to check Vowel or Consonant in C using Switch Statement:

We have earlier discussed that, If you don’t specify the break after each case, The subsequent case also executes. This often causes bugs but it is also useful to create operator OR like behavior.

To understand better let’s look at the following program.

We are going to use the switch case to detect the Vowels and Consonants.

Program Output:

We used isalpha() from ctypes.h and the switch case to check the vowels and consonants.

As you can see from the code, We have intentionally not used break statement after the cases a, e, i , o and u and upper case A, E, I, O, and Finally used break statement after the case U. So It behaves like a OR operation.

C Tutorials Index:

Venkat

Hi Guys, I am Venkatesh. I am a programmer and an Open Source enthusiast. I write about programming and technology on this blog.

You may also like...

4 Responses

  1. […] C Program to check character is Vowel or Consonant […]

  2. […] C Program to check character is Vowel or Consonant […]

  3. […] C Program to find whether the alphabet is a Vowel or Consonant using Switch Case […]

  4. […] C Program to check character is Vowel or Consonant […]

Leave a Reply