VOOZH about

URL: https://www.geeksforgeeks.org/dsa/program-find-character-vowel-consonant/

⇱ Program to find if a character is vowel or Consonant - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Program to find if a character is vowel or Consonant

Last Updated : 28 Apr, 2026

Given a character ch representing an English alphabet, determine whether it is a vowel or not. Return true if ch is a vowel otherwise return false.

Examples:  

Input: ch = 'a'
Output: true
Explanation: 'a' is a vowel. So output for this test case is true.

Input: ch = 'Z'
Output: false
Explanation: 'Z' is not a vowel. So output for this test case is false.

👁 vowel_or_Consonant

Using Direct Character Comparison (Handling Uppercase & Lowercase) - O(1) Time O(1) Space

The Idea is to Directly compare the given character with all vowels (both lowercase and uppercase). If it matches any, return true; otherwise return false.

Algorithm:

  • Take input character ch.
  • Check if ch is equal to 'a', 'e', 'i', 'o', 'u' or 'A', 'E', 'I', 'O', 'U'.
  • If any condition matches -> return true.
  • Otherwise -> return false.

Output
true

Time Complexity: O(1)
Auxiliary Space: O(1)

Using Bitmasking Technique - O(1) Time O(1) Space

Both lowercase and uppercase vowels share the same last 5 bits (LSBs) in their ASCII representation. This allows us to normalize any character using (ch & 0x1F) and map it to a small range of values.

We use a precomputed bitmask 0x208222, which is designed such that when it is right-shifted by the normalized value (ch & 0x1F), it produces 1 at the least significant bit for vowel positions (1, 5, 9, 15, 21), and 0 for all other characters.

Thus, by evaluating:

( 0 x 208222 >> (ch & 0 x 1F)) & 1

ASCII Values of Vowels (Lowercase and Uppercase):

👁 Values_of_Vowels

Algorithm:

  • Take input character ch.
  • Compute (ch & 0x1F) to get last 5 bits.
  • Right shift 0x208222 by this value.
  • Apply & 1 to extract the result.
  • If result is 1 → return true (vowel).
  • Else → return false (not a vowel).

Output
true

Time Complexity: O(1)
Auxiliary Space: O(1)

Comment