Program to Check Vowel or Consonant Using Switch Case

Posted in

Program to Check Vowel or Consonant Using Switch Case
vinaykhatri

Vinay Khatri
Last updated on April 14, 2024

    Problem Statement

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

    For example

    Input:
    a
    Output
    It is a Vowel
    

    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.

    Let us now implement the switch case statement to check whether a letter is a vowel or not in different programming languages.

    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 Vowel or Consonant 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 Vowel or Consonant 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.

    Without Switch Case

    # 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-------------

    With Switch Case

    Python 3.10 introduced the feature of a switch case called structural pattern matching .

    Let us use it to implement the same program.

    char = input("Enter an Alphabet: ")
    
    def is_vowel(char):
        vowels = ['A', 'a', 'E', 'e', 'I', 'i', 'O', 'o', 'U', 'u']
    
        if char in vowels:
            return "----------------It is a Vowel-------------"
        else:
            return "++++++++++++++It is not a Vowel+++++++++++"
    
    result = is_vowel(char)
    print(result)

    Output

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

    Java Program to Check Vowel or Consonant Using Switch Case

    import java.util.*;
    class Main
    {
        public static void main(String[] args) 
        {
    	    char chr;
    	    Scanner sc = new Scanner(System.in);
            System.out.print("Enter an Alphabet: ");
            chr = sc.next().charAt(0);
    
            switch(chr)
                {
                    case 'a':
                    case 'A':
    
                    case 'e':
                    case 'E':
    
                    case 'i':
                    case 'I':
    
                    case 'o':
                    case 'O':
    
                    case 'u':
                    case 'U':
                    System.out.println("----------------It is a Vowel-------------");
                    break;
                    default:
                    System.out.println("++++++++++++++It is not a Vowel+++++++++++");
                }
        }
    }

    Output

    Enter an Alphabet: r
    ++++++++++++++It is not a Vowel+++++++++++

    Wrapping Up!

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

    This program is a good example to learn about the switch statement.

    People are also reading:

    Leave a Comment on this Post

    0 Comments