Skip to content

← Back to Overview

Vowel or Consonant Check

Concept Explanation

What is it?

This program classifies a single character input as either a vowel, a consonant, or neither (if it's not an alphabet). Vowels are typically 'a', 'e', 'i', 'o', 'u' (and their uppercase counterparts), while consonants are all other alphabetic characters.

Why is it important?

Character classification is a fundamental skill in text processing and string manipulation. This particular check is a classic example for demonstrating conditional logic, character comparison, and handling different cases (uppercase/lowercase). It serves as a building block for more complex linguistic processing.

Where is it used?

  • Text Analysis: Simple linguistic analysis, like counting vowels or consonants in a text.
  • Phonetics and Linguistics: Basic processing of speech sounds or language structure.
  • Educational Tools: Teaching basic programming logic and character handling.
  • Input Validation: Ensuring specific patterns in text fields (though less common for this exact check).

Real-world example

Consider a children's game that involves identifying letters. This program's logic could be a core part of an interactive tool that tells a child, "Yes, that's a vowel!" or "That's a consonant!" when they type a letter.


Algorithm

  1. Start.
  2. Get a single character (ch) from the user.
  3. Convert ch to lowercase for easier comparison.
  4. Check if ch is an alphabet (a-z). a. If ch is an alphabet: i. Check if ch is one of 'a', 'e', 'i', 'o', 'u'. ii. If true, ch is a vowel. iii. Else, ch is a consonant. b. Else (ch is not an alphabet): i. Display that ch is not an alphabet.
  5. Display the result.
  6. End.

Edge Cases: - Inputting more than one character (handled by taking only the first character or explicit error). - Numbers, symbols, and whitespace: These should be correctly identified as non-alphabetic. - Case sensitivity: Handled by converting input to lowercase or checking both upper and lower case ranges.


Implementations

import java.util.Scanner;

public class VowelOrConsonant {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.print("Enter a character: ");
        char ch = scanner.next().charAt(0);

        System.out.println("Input Character: " + ch);

        // Convert to lowercase to handle both cases easily
        char lowerCaseCh = Character.toLowerCase(ch);

        if (lowerCaseCh >= 'a' && lowerCaseCh <= 'z') { // Check if it's an alphabet
            if (lowerCaseCh == 'a' || lowerCaseCh == 'e' || lowerCaseCh == 'i' || lowerCaseCh == 'o' || lowerCaseCh == 'u') {
                System.out.println("The character '" + ch + "' is a VOWEL.");
            } else {
                System.out.println("The character '" + ch + "' is a CONSONANT.");
            }
        } else {
            System.out.println("The character '" + ch + "' is NOT an alphabet.");
        }

        scanner.close();
    }
}
# Get a character from user
char_input = input("Enter a character: ")

# Ensure only one character is processed
if len(char_input) == 1:
    character = char_input[0]

    # Convert to lowercase for easier comparison
    lower_char = character.lower()

    if lower_char.isalpha(): # Check if it's an alphabet first
        if lower_char in ('a', 'e', 'i', 'o', 'u'):
            print(f"The character '{character}' is a VOWEL.")
        else:
            print(f"The character '{character}' is a CONSONANT.")
    else:
        print(f"The character '{character}' is NOT an alphabet.")
else:
    print("Please enter a single character.")
#include <stdio.h>
#include <ctype.h> // For isalpha() and tolower()

int main() {
    char ch;

    printf("Enter a character: ");
    scanf(" %c", &ch); // Space before %c to consume any leftover newline character

    printf("Input Character: '%c'", ch);

    // Convert to lowercase to handle both cases easily
    char lower_ch = tolower(ch);

    if (isalpha(ch)) { // Check if it's an alphabet first
        if (lower_ch == 'a' || lower_ch == 'e' || lower_ch == 'i' || lower_ch == 'o' || lower_ch == 'u') {
            printf("The character '%c' is a VOWEL.", ch);
        } else {
            printf("The character '%c' is a CONSONANT.", ch);
        }
    } else {
        printf("The character '%c' is NOT an alphabet.", ch);
    }

    return 0;
}
SET SERVEROUTPUT ON;
DECLARE
  input_char  CHAR(1) := '&Enter_a_Character';
  lower_char  CHAR(1);
BEGIN
  DBMS_OUTPUT.PUT_LINE('Input Character: ' || input_char);

  lower_char := LOWER(input_char); -- Convert to lowercase

  IF    (lower_char >= 'a' AND lower_char <= 'z') THEN -- Check if it's an alphabet
    IF    lower_char = 'a'
       OR lower_char = 'e'
       OR lower_char = 'i'
       OR lower_char = 'o'
       OR lower_char = 'u' THEN
      DBMS_OUTPUT.PUT_LINE('The character "' || input_char || '" is a VOWEL.');
    ELSE
      DBMS_OUTPUT.PUT_LINE('The character "' || input_char || '" is a CONSONANT.');
    END IF;
  ELSE
    DBMS_OUTPUT.PUT_LINE('The character "' || input_char || '" is NOT an alphabet.');
  END IF;
END;
/

Explanation

  • Java: Uses Character.toLowerCase() to normalize the input and then checks against vowel characters. Character.isLetter() can be used for initial alphabet check.
  • Python: Uses str.lower() to convert to lowercase and str.isalpha() to check for alphabetic nature. Membership operator (in) is used for efficient vowel checking.
  • C: Employs tolower() from <ctype.h> for case-insensitive comparison and isalpha() for checking if it's an alphabet.
  • Oracle: Implemented in PL/SQL. LOWER() function converts to lowercase. Direct character comparisons are used within IF-ELSIF-ELSE structure.

Complexity Analysis

  • Time Complexity: O(1) - The number of comparisons is constant.
  • Space Complexity: O(1) - A fixed number of variables are used.

Flowchart

graph TD
    A[Start] --> B[Get Character ch]
    B --> C[Convert ch to lowercase]
    C --> D{ch is an alphabet?}
    D -- Yes --> E{ch is 'a', 'e', 'i', 'o', or 'u'?}
    E -- Yes --> F[Display VOWEL]
    E -- No --> G[Display CONSONANT]
    F --> H[End]
    G --> H
    D -- No --> I[Display NOT an alphabet]
    I --> H

Sample Dry Run

Step ch lower_ch isAlpha? isVowel? Description
Input 'E' - - - User enters 'E'
Process 'E' 'e' - - Convert 'E' to 'e'
Decision 'e' 'e' True - 'e' is an alphabet
Decision 'e' 'e' True True 'e' is a vowel
Output - - - - Display "'E' is a VOWEL."
End - - - - Program terminates
Input 'z' - - - User enters 'z'
Process 'z' 'z' - - Convert 'z' to 'z'
Decision 'z' 'z' True - 'z' is an alphabet
Decision 'z' 'z' True False 'z' is not a vowel
Output - - - - Display "'z' is a CONSONANT."
End - - - - Program terminates
Input '5' - - - User enters '5'
Process '5' '5' - - Convert '5' to '5'
Decision '5' '5' False - '5' is not an alphabet
Output - - - - Display "'5' is NOT an alphabet."
End - - - - Program terminates

Practice Problems

Easy

  • Modify the program to count the number of vowels and consonants in a given string.
  • Check if a word starts with a vowel or a consonant.

Medium

  • Implement a simple Caesar cipher (encryption) that shifts alphabetic characters while preserving non-alphabetic ones.
  • Write a program that takes a sentence and converts it to "Pig Latin" (moves the first consonant cluster to the end of the word and adds "ay").

Hard

  • Develop a program that analyzes text for readability scores, which often involve counting vowels and consonants.

"Learning to code is learning to create, and creating is the highest form of expression." - Anonymous