Program to Check Vowel or Consonant Using Switch Case

Posted in

Program to Check Vowel or Consonant Using Switch Case

Vinay Khatri
Last updated on November 9, 2022

    Problem Statement

    Switch case is a programming syntax, that provides an alternative elegant way to represent conditional base statements in programming languages. Mostly all the popular programming languages support switch-case statements.

    Here, we have a problem statement where we need to write a script or program in C++, C, and Python that checks if a user-entered character or alphabet is a vowel or not.

    For example

    Input:
    a
    Output
    It is a Vowel
    

    C Program to Check Vowel or Consonant Using Switch Case

    #include <stdio.h>
    int main()
    {	
    	char chr;
    	
    	//input chracter
    	printf("Enter the Alphabet: ");
    	scanf("%c", &chr);
    	
    	switch(chr)
                {
                    case 'a':
                    case 'A':
    
                    case 'e':
                    case 'E':
    
                    case 'i':
                    case 'I':
    
                    case 'o':
                    case 'O':
    
                    case 'u':
                    case 'U':
                    	printf("----------------It is a Vowel-------------");
                   		break;
                    default:
                    	printf("++++++++++++++It is not a Vowel+++++++++++");
           		 }
    	
    	return 0;
    }

    Output

    Enter the Alphabet: B
    ++++++++++++++It is not a Vowel+++++++++++

    C++ Program to check given Alphabet is a vowel or not using switch case:

    #include<iostream>
    using namespace std;
    int main()
    {
    	char chr;
        cout<<"Enter an Alphabet: "; cin>>chr;
    
        switch(chr)
                    {
                    case 'a':
                    case 'A':
    
                    case 'e':
                    case 'E':
    
                    case 'i':
                    case 'I':
    
                    case 'o':
                    case 'O':
    
                    case 'u':
                    case 'U':
                    cout<<"----------------It is a Vowel-------------";
                    break;
                    default:
                    cout<<"++++++++++++++It is not a Vowel+++++++++++";
            }
    	
    	return 0;
    }

    Output:

    Enter an Alphabet: A
    
    ----------------It is a Vowel------------

    Python Program to check given Alphabet is a vowel or not using switch case:

    Unfortunately, Python 3.9 and older versions do not support the switch-case statement, so instead of the switch case, we can use Python if..else statements. To know what are the alternative ways to implement switch cases in Python click here .

    # input alphabet
    char = input("Enter an Alphabet: ")
    
    # check for the VOWEL
    if char in 'aeiouAEIOU':
        print("----------------It is a Vowel-------------")
    else:
        print("++++++++++++++It is not a Vowel+++++++++++")

    Output

    Enter an Alphabet: I
    ----------------It is a Vowel-------------

    Wrapping Up!

    Writing a program to check if the user entered character is an alphabet or not is a trivial task, and in the above tutorial we have demonstrated how can you use C, and C++ switch-case statements to solve the problem.

    By looking at the program, even a non-programmer can tell what the program is supposed to do, that's what switch-case provides to the programming language. For Python, we do not have support for switch-case so we implemented the program using if..else statement.

    People are also reading:

    Leave a Comment on this Post

    0 Comments