Check whether a character is a vowel or consonant in Java

Understanding Vowels and Consonants

A vowel is a letter (a, e, i, o, u) that represents an open speech sound. A consonant is any other letter in the alphabet.

We will explore three different methods to check whether a character is a vowel or consonant in Java.

Method 1: Using if-else

This method checks if the character belongs to the set of vowels.

import java.util.Scanner;

public class VowelConsonantCheck {
    public static void checkVowelOrConsonant(char ch) {
        if ("AEIOUaeiou".indexOf(ch) != -1) {
            System.out.println(ch + " is a vowel");
        } else {
            System.out.println(ch + " is a consonant");
        }
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter a character: ");
        char ch = scanner.next().charAt(0);
        checkVowelOrConsonant(ch);
        scanner.close();
    }
}
            
Input: a
Output: a is a vowel

Method 2: Using Switch Case

This method uses a switch case to determine if the character is a vowel.

import java.util.Scanner;

public class VowelConsonantSwitch {
    public static void checkVowelOrConsonant(char ch) {
        switch (Character.toLowerCase(ch)) {
            case 'a': case 'e': case 'i': case 'o': case 'u':
                System.out.println(ch + " is a vowel");
                break;
            default:
                System.out.println(ch + " is a consonant");
        }
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter a character: ");
        char ch = scanner.next().charAt(0);
        checkVowelOrConsonant(ch);
        scanner.close();
    }
}
            
Input: b
Output: b is a consonant

Method 3: Using Recursion

This method checks vowels recursively.

import java.util.Scanner;

public class VowelConsonantRecursion {
    public static boolean isVowel(char ch) {
        return "AEIOUaeiou".indexOf(ch) != -1;
    }

    public static void checkVowelOrConsonant(char ch) {
        if (isVowel(ch)) {
            System.out.println(ch + " is a vowel");
        } else {
            System.out.println(ch + " is a consonant");
        }
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter a character: ");
        char ch = scanner.next().charAt(0);
        checkVowelOrConsonant(ch);
        scanner.close();
    }
}
            
Input: o
Output: o is a vowel
Strings

Below You will find some of the most important codes in languages like C, C++, Java, and Python. These codes are of prime importance for college semester exams and online tests.

Getting Started

Check whether a character is a vowel or consonant: C C++ Java Python

Check whether a character is an alphabet or not: C C++ Java Python

Find the ASCII value of a character: C C++ Java Python

Length of the string without using strlen() function: C C++ Java Python

Toggle each character in a string: C C++ Java Python

Count the number of vowels: C C++ Java Python

Remove the vowels from a string: C C++ Java Python

Check if the given string is Palindrome or not: C C++ Java Python

Print the given string in reverse order: C C++ Java Python

Remove all characters from string except alphabets: C C++ Java Python

Remove spaces from a string: C C++ Java Python

Replace a sub-string in a string: C C++ Java Python

Count common sub-sequences in two strings: C C++ Java Python

Compare two strings with wildcard support in one of them: C C++ Java Python

List all permutations of a given string in dictionary order: C C++ Java Python

Operations on Strings: C C++ Java Python