Switch-Case Magic: Checking Vowels and Consonants in C



In this code I used only one variable for inputting a alphabetical character from user and I used switch cases for checking vowels. I had written all the possible outcomes of vowels and printed that the character is vowel, and the default case prints character is consonant or character is not an alphabet. the switch case ends and run perfectly.

 

Main Keywords and variables used in this code:

1.We included stdio.h, conio.h.

2.then, we use void keyword and main function to run this code.

3.then, we defined variables char.

4.then, we inputted an alphabetic character from user.

5.then, we use switch and case statements for printing the vowel.

6.then, we use getch for viewing the result.

7.and then the code ends. 






If you'd like to use this code, feel free to copy it from here:


#include <stdio.h>
#include <conio.h>

void main()
{
    char character;

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

    switch (character)
    {
    case 'A':
        printf("%c is a Vowel.\n", character);
        break;
    case 'E':
        printf("%c is a Vowel.\n", character);
        break;
    case 'I':
        printf("%c is a Vowel.\n", character);
        break;
    case 'O':
        printf("%c is a Vowel.\n", character);
        break;
    case 'U':
        printf("%c is a Vowel.\n", character);
        break;
    case 'a':
        printf("%c is a Vowel.\n", character);
        break;
    case 'e':
        printf("%c is a Vowel.\n", character);
        break;
    case 'i':
        printf("%c is a Vowel.\n", character);
        break;
    case 'o':
        printf("%c is a Vowel.\n", character);
        break;
    case 'u':
        printf("%c is a Vowel.\n", character);
        break;
   
    default:
        if ( (character >= 'a' && character <= 'z')|| (character >= 'A' && character <= 'Z'))
            printf("%c is a consonant.", character);
        else
        {
            printf("%c is not an Alphabet.\n", character);
        }
   
        break;
    }
    getch();
}

Comments