Vowel or Consonant

beginner Programming Basics

Problem

Read one character and print Vowel if it is one of a, e, i, o, u (capital or small), Consonant if it is any other English letter, and Not a letter if it is not a letter.

Constraints

  • One character

Examples

Input

e

Output

Vowel

Input

z

Output

Consonant

Solutions

Best approachTime O(1)Space O(1)

First we check that the character is a letter. Then we make it lower case, so that A and a are handled in the same way, and we test the five vowels.

Same code works in Java810172125Basic features only, so nothing changes between versions.
/*
 * Problem : Check if a character is a vowel or a consonant.
 * Input   : One character.
 * Output  : Vowel, Consonant or Not a letter.
 * Approach: Make the letter lower case and test the five vowels.
 * Time    : O(1)
 * Space   : O(1)
 * Java    : 8 or newer.
 */
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        char letter = new Scanner(System.in).next().charAt(0);

        // Step 1: Check that the character is an English letter. Digits and symbols are not letters.
        boolean isLetter = (letter >= 'a' && letter <= 'z') || (letter >= 'A' && letter <= 'Z');
        if (!isLetter) {
            System.out.println("Not a letter");
            return;
        }

        // Step 2: Make the letter lower case. This way we need to test only five vowels.
        char lower = Character.toLowerCase(letter);

        // Step 3: Test the five vowels. All other letters are consonants.
        if (lower == 'a' || lower == 'e' || lower == 'i' || lower == 'o' || lower == 'u') {
            System.out.println("Vowel");
        } else {
            System.out.println("Consonant");
        }
    }
}
Hints (2)
  1. Is the digit 5 a consonant?
  2. How can you avoid writing ten vowel tests?