C program to check if a character is a vowel or consonant

C program to check if a character is a vowel or consonant:

In this post, we will learn how to check if character is vowel or consonant. Characters a, e, i, o, u are called vowels and other characters are called consonant.

Our program will check for both lowecase and uppwecase characters, i.e. if the entered character is a, e, i, o, u or A, E, I, O, U, it will detect it as vowel, else consonant.

I will show you two different ways to solve this problem : Using if-else block and using switch statements.

With this program, you will learn how to take user inputs in C and how to use if-else and switch blocks.

C program:

Below is the complete C program :

#include <stdio.h>

int main()
{
    char givenChar;

    printf("Enter a character : ");
    scanf("%c", &givenChar);

    if (givenChar == 'a' || givenChar == 'e' || givenChar == 'i' ||
        givenChar == 'o' || givenChar == 'u' || givenChar == 'A' ||
        givenChar == 'E' || givenChar == 'I' || givenChar == 'O' ||
        givenChar == 'U')
    {
        printf("Entered character is vowel");
    }
    else
    {
        printf("Entered character is consonant");
    }

    return 0;
}

It will print:

Enter a character : A                 
Entered character is vowel

Enter a character : x
Entered character is consonant

Explanation:

In this program:

  • givenChar is a char variable to store the user input character.
  • Using printf, we are asking the user to enter a character and using scanf we are reading the character and storing it in givenChar variable.
  • The if block compares the value of givenChar with all vowels. It checks for both lower case and upper case vowels and if it equals to any one of them, it enters the if block and prints the message.
  • If it is not vowel, it enters the else block.

C program to check vowel or consonant using a switch case statement:

We can also solve it by using a switch statement:

#include <stdio.h>

int main()
{
    char givenChar;
    int isVowel = 0;

    printf("Enter a character :");
    scanf("%c", &givenChar);

    switch (givenChar)
    {
    case 'a':
        isVowel = 1;
        break;
    case 'e':
        isVowel = 1;
        break;
    case 'i':
        isVowel = 1;
        break;
    case 'o':
        isVowel = 1;
        break;
    case 'u':
        isVowel = 1;
        break;
    case 'A':
        isVowel = 1;
        break;
    case 'E':
        isVowel = 1;
        break;
    case 'I':
        isVowel = 1;
        break;
    case 'O':
        isVowel = 1;
        break;
    case 'U':
        isVowel = 1;
        break;
    }

    if (isVowel == 1)
    {
        printf("Entered character is vowel");
    }
    else
    {
        printf("Entered character is consonant");
    }
    return 0;
}

This is almost similar to the above program.

We created one variable isVowel and assigned it as 0. The switch-case statement changes it to 1 if it finds the entered character vowel.

Based on the value of isVowel, we are printing the message.

You might also like: