Program to find if a character is vowel or Consonant
Last Updated :
16 Feb, 2023
Given a character, check if it is vowel or consonant. Vowels are 'a', 'e', 'i', 'o' and 'u'. All other characters ('b', 'c', 'd', 'f' ....) are consonants.

Examples:
Input : x = 'c'
Output : Consonant
Input : x = 'u'
Output : Vowel
We check whether the given character matches any of the 5 vowels. If yes, we print "Vowel", else we print "Consonant".
C++
// CPP program to check if a given character
// is vowel or consonant.
#include <iostream>
using namespace std;
// Function to check whether a character is
// vowel or not
void vowelOrConsonant(char x)
{
if (x == 'a' || x == 'e' || x == 'i' ||
x == 'o' || x == 'u')
cout << "Vowel" << endl;
else
cout << "Consonant" << endl
}
// Driver code
int main()
{
vowelOrConsonant('c');
vowelOrConsonant('e');
return 0;
}
Java
// java program to check if a given
// character is vowel or consonant.
import java.io.*;
public class GFG {
// Function to check whether a
// character is vowel or not
static void vowelOrConsonant(char x)
{
if (x == 'a' || x == 'e' || x == 'i' ||
x == 'o' || x == 'u')
System.out.println("Vowel");
else
System.out.println("Consonant");
}
// Driver code
static public void main(String[] args)
{
vowelOrConsonant('c');
vowelOrConsonant('e');
}
}
// This code is contributed by vt_m.
Python3
# Python3 program to check if a given
# character is vowel or consonant.
# Function to check whether a character
# is vowel or not
def vowelOrConsonant(x):
if (x == 'a' or x == 'e' or
x == 'i' or x == 'o' or x == 'u'):
print("Vowel")
else:
print("Consonant")
# Driver code
vowelOrConsonant('c')
vowelOrConsonant('e')
# This code is contributed by
# mohit kumar 29
C#
// C# program to check if a given
// character is vowel or consonant.
using System;
public class GFG {
// Function to check whether a
// character is vowel or not
static void vowelOrConsonant(char x)
{
if (x == 'a' || x == 'e' || x == 'i' ||
x == 'o' || x == 'u')
Console.WriteLine("Vowel");
else
Console.WriteLine("Consonant");
}
// Driver code
static public void Main()
{
vowelOrConsonant('c');
vowelOrConsonant('e');
}
}
// This code is contributed by vt_m.
PHP
<?php
// PHP program to check if a given
// character is vowel or consonant.
// Function to check whether a
// character is vowel or not
function vowelOrConsonant($x)
{
if ($x == 'a' || $x == 'e' ||
$x == 'i' || $x == 'o' ||
$x == 'u')
echo "Vowel" . "\n";
else
echo "Consonant" . "\n";
}
// Driver code
vowelOrConsonant('c');
vowelOrConsonant('e');
// This code is contributed
// by Akanksha Rai
?>
JavaScript
<script>
// Javascript program to check if a given
// character is vowel or consonant.
// Function to check whether a
// character is vowel or not
function vowelOrConsonant(x)
{
if (x == 'a' || x == 'e' || x == 'i' || x == 'o' || x == 'u')
document.write("Vowel" + "</br>");
else
document.write("Consonant" + "</br>");
}
vowelOrConsonant('c');
vowelOrConsonant('e');
// This code is contributed by mukesh07.
</script>
Output:
Consonant
Vowel
Time Complexity: O(1)
Auxiliary Space: O(1)
How to handle capital letters as well?
C++
// C++ program to check if a given character
// is vowel or consonant.
#include <iostream>
using namespace std;
// Function to check whether a character is
// vowel or not
void vowelOrConsonant(char x)
{
if (x == 'a' || x == 'e' || x == 'i' ||
x == 'o' || x == 'u' || x == 'A' ||
x == 'E' || x == 'I' || x == 'O' || x == 'U')
cout << "Vowel" << endl;
else
cout << "Consonant" << endl;
}
// Driver code
int main()
{
vowelOrConsonant('c');
vowelOrConsonant('E');
return 0;
}
Java
// Java program to check if a given
// character is vowel or consonant
import java.io.*;
public class GFG {
// Function to check whether a
// character is vowel or not
static void vowelOrConsonant(char x)
{
if (x == 'a' || x == 'e' || x == 'i' ||
x == 'o' || x == 'u' || x == 'A' ||
x == 'E' || x == 'I' || x == 'O' || x == 'U')
System.out.println("Vowel");
else
System.out.println("Consonant");
}
// Driver code
static public void main(String[] args)
{
vowelOrConsonant('c');
vowelOrConsonant('E');
}
}
//
Python3
# Python3 program to check if a given
# character is vowel or consonant.
# Function to check whether a
# character is vowel or not
def vowelOrConsonant(x):
if (x == 'a' or x == 'e' or x == 'i' or
x == 'o' or x == 'u' or x == 'A' or
x == 'E' or x == 'I' or x == 'O' or
x == 'U'):
print("Vowel")
else:
print("Consonant")
# Driver code
if __name__ == '__main__':
vowelOrConsonant('c')
vowelOrConsonant('E')
# This code is contributed by
# Sanjit_Prasad
C#
// C# program to check if a given
// character is vowel or consonant
using System;
public class GFG {
// Function to check whether a
// character is vowel or not
static void vowelOrConsonant(char x)
{
if (x == 'a' || x == 'e' || x == 'i' ||
x == 'o' || x == 'u' || x == 'A' ||
x == 'E' || x == 'I' || x == 'O' || x == 'U')
Console.WriteLine("Vowel");
else
Console.WriteLine("Consonant");
}
// Driver code
static public void Main()
{
vowelOrConsonant('c');
vowelOrConsonant('E');
}
}
// This article is contributed by vt_m.
PHP
<?php
// PHP program to check if a given character
// is vowel or consonant.
// Function to check whether a
// character is vowel or not
function vowelOrConsonant($x)
{
if ($x == 'a' || $x == 'e' || $x == 'i' ||
$x == 'o' || $x == 'u' || $x == 'A' ||
$x == 'E' || $x == 'I' || $x == 'O' ||
$x == 'U')
echo "Vowel" . "\n";
else
echo "Consonant" . "\n";
}
// Driver code
vowelOrConsonant('c');
vowelOrConsonant('E');
// This code is contributed
// by Akanksha Rai
?>
JavaScript
<script>
// Javascript program to check if a
// given character is vowel or consonant.
// Function to check whether a character is
// vowel or not
function vowelOrConsonant(x)
{
if (x == 'a' || x == 'e' || x == 'i' ||
x == 'o' || x == 'u' || x == 'A' ||
x == 'E' || x == 'I' || x == 'O' || x == 'U')
document.write("Vowel" + "</br>");
else
document.write("Consonant" + "</br>");
}
// Driver code
vowelOrConsonant('c');
vowelOrConsonant('E');
// This code is contributed by rameshtravel07
</script>
Output:
Consonant
Vowel
Time Complexity: O(1)
Auxiliary Space: O(1)
using switch case
C++
#include <iostream>
using namespace std;
int isVowel(char ch)
{
int check = 0;
switch (ch) {
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
case 'A':
case 'E':
case 'I':
case 'O':
case 'U':
check = 1;
}
return check;
}
// Driver Code
int main()
{
cout << "a is " << isVowel('a')
<< endl; // 1 means Vowel
cout << "x is " << isVowel('x')
<< endl; // 0 means Consonant
return 0;
}
C
#include <stdio.h>
int isVowel(char ch)
{
int check = 0;
switch (ch) {
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
case 'A':
case 'E':
case 'I':
case 'O':
case 'U':
check = 1;
}
return check;
}
int main()
{
printf("a is %d\n",
isVowel('a')); // 1 means Vowel
printf("x is %d\n",
isVowel('x')); // 0 means Consonant
return 0;
}
Java
/*package whatever //do not write package name here */
import java.io.*;
class GFG {
static int isVowel(char ch)
{
int check = 0;
switch (ch) {
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
case 'A':
case 'E':
case 'I':
case 'O':
case 'U':
check = 1;
}
return check;
}
// Driver Code
public static void main(String[] args)
{
System.out.println("a is " + isVowel('a'));
System.out.println("x is " + isVowel('x'));
}
}
Python3
def isVowel(ch):
switcher = {
'a': "Vowel",
'e': "Vowel",
'i': "Vowel",
'o': "Vowel",
'u': "Vowel",
'A': "Vowel",
'E': "Vowel",
'I': "Vowel",
'O': "Vowel",
'U': "Vowel"
}
return switcher.get(ch, "Consonant")
# Driver Code
print('a is '+isVowel('a'))
print('x is '+isVowel('x'))
C#
using System;
class GFG
{
static int isVowel(char ch)
{
int check = 0;
switch (ch) {
case 'a':
check = 1;
break;
case 'e':
check = 1;
break;
case 'i':
check = 1;
break;
case 'o':
check = 1;
break;
case 'u':
check = 1;
break;
case 'A':
check = 1;
break;
case 'E':
check = 1;
break;
case 'I':
check = 1;
break;
case 'O':
check = 1;
break;
case 'U':
check = 1;
break;
}
return check;
}
// Driver code
static public void Main ()
{
Console.WriteLine("a is " + isVowel('a'));
Console.WriteLine("x is " + isVowel('x'));
}
}
// This code is contributed by rag2127
JavaScript
<script>
function isVowel(ch)
{
var check = 0;
switch (ch) {
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
case 'A':
case 'E':
case 'I':
case 'O':
case 'U':
check = 1;
}
return check;
}
// Driver Code
// 1 means Vowel
document.write("a is " + isVowel('a') + "<br>");
// 0 means Consonant
document.write("x is " + isVowel('x') + "<br>");
// This code is contributed by Shivani
</script>
Time Complexity: O(1)
Auxiliary Space: O(1)
Another way is to find() the character in a string containing only Vowels.
C++
#include <iostream>
#include <string>
using namespace std;
int isVowel(char ch)
{
// Make the list of vowels
string str = "aeiouAEIOU";
return (str.find(ch) != string::npos);
}
// Driver code
int main()
{
cout << "a is " << isVowel('a') << endl;
cout << "x is " << isVowel('x') << endl;
return 0;
}
C
#include <stdio.h>
#include <string.h>
int isVowel(char ch)
{
// Make the list of vowels
char str[] = "aeiouAEIOU";
return (strchr(str, ch) != NULL);
}
// Driver Code
int main()
{
printf("a is %d\n", isVowel('a'));
printf("x is %d\n", isVowel('x'));
return 0;
}
Java
/*package whatever //do not write package name here */
import java.io.*;
class GFG {
static int isVowel(char ch)
{
// Make the list of vowels
String str = "aeiouAEIOU";
return (str.indexOf(ch) != -1) ? 1 : 0;
}
// Driver Code
public static void main(String[] args)
{
System.out.println("a is " + isVowel('a'));
System.out.println("x is " + isVowel('x'));
}
}
Python3
def isVowel(ch):
# Make the list of vowels
str = "aeiouAEIOU"
return (str.find(ch) != -1)
# Driver Code
print('a is '+str(isVowel('a')))
print('x is '+str(isVowel('x')))
C#
// C# program to implement
// the above approach
using System;
class GFG{
static int isVowel(char ch)
{
// Make the list of vowels
string str = "aeiouAEIOU";
return (str.IndexOf(ch) != -1) ?
1 : 0;
}
// Driver code
static void Main()
{
Console.WriteLine("a is " + isVowel('a'));
Console.WriteLine("x is " + isVowel('x'));
}
}
// This code is contributed by divyeshrabadiya07
JavaScript
<script>
/*package whatever //do not write package name here */
function isVowel(ch)
{
// Make the list of vowels
let str = "aeiouAEIOU";
return (str.indexOf(ch) != -1) ? 1 : 0;
}
// Driver Code
document.write("a is " + isVowel('a')+"<br>");
document.write("x is " + isVowel('x')+"<br>");
// This code is contributed by patel2127
</script>
Time Complexity: O(N)
Auxiliary Space: O(1)
Most efficient way to check Vowel using bit shift :
In ASCII these are the respective values of every vowel both in lower and upper cases.
Vowel | DEC | HEX | BINARY |
a
| 97 | 0x61 | 01100001 |
e
| 101 | 0x65 | 01100101 |
i
| 105 | 0x69 | 01101001 |
o
| 111 | 0x6F | 01101111 |
u
| 117 | 0x75 | 01110101 |
|
A
| 65 | 0x41 | 01000001 |
E
| 69 | 0x45 | 01000101 |
I
| 73 | 0x49 | 01001001 |
O
| 79 | 0x4F | 01001111 |
U
| 85 | 0x55 | 01010101 |
As very lower and upper case vowels have the same 5 LSBs. We need a number 0x208222 which gives 1 in its LSB after right-shift 1, 5, 19, 15 otherwise gives 0. The numbers depend on character encoding.
DEC | HEX | BINARY |
31 | 0x1F | 00011111 |
2130466 | 0x208222 | 1000001000001000100010 |
C++
#include <iostream>
using namespace std;
int isVowel(char ch)
{
return (0x208222 >> (ch & 0x1f)) & 1;
// same as (2130466 >> (ch & 31)) & 1;
}
// Driver Code
int main()
{
cout << "a is " << isVowel('a') << endl;
cout << "x is " << isVowel('x') << endl;
return 0;
}
C
#include <stdio.h>
int isVowel(char ch)
{
return (0x208222 >> (ch & 0x1f)) & 1;
// same as (2130466 >> (ch & 31)) & 1;
}
// Driver Code
int main()
{
printf("a is %d\n", isVowel('a'));
printf("x is %d\n", isVowel('x'));
return 0;
}
Java
/*package whatever //do not write package name here */
import java.io.*;
class GFG
{
static int isVowel(char ch)
{
return (0x208222 >> (ch & 0x1f)) & 1;
// same as (2130466 >> (ch & 31)) & 1;
}
// Driver Code
public static void main(String[] args)
{
System.out.println("a is " + isVowel('a'));
System.out.println("x is " + isVowel('x'));
}
}
Python3
def isVowel(ch):
return (0x208222 >> (ord(ch) & 0x1f)) & 1
# same as (2130466 >> (ord(ch) & 31)) & 1;
# Driver Code
print('a is '+str(isVowel('a')))
print('x is '+str(isVowel('x')))
C#
// C# implementation of above approach
using System;
class GFG {
static int isVowel(char ch)
{
return (0x208222 >> (ch & 0x1f)) & 1;
// same as (2130466 >> (ch & 31)) & 1;
}
// Driver code
static void Main()
{
Console.WriteLine("a is " + isVowel('a'));
Console.WriteLine("x is " + isVowel('x'));
}
}
// This code is contributed by divesh072019
JavaScript
<script>
function isVowel(ch)
{
return (0x208222 >> (ch.charCodeAt(0) & 0x1f)) & 1;
// same as (2130466 >> (ch & 31)) & 1;
}
// Driver Code
document.write("a is " + isVowel('a')+"<br>");
document.write("x is " + isVowel('x')+"<br>");
// This code is contributed by unknown2108
</script>
Time Complexity: O(1)
Auxiliary Space: O(1)
*We can omit the ( ch & 0x1f ) part on X86 machines as the result of SHR/SAR (which is >> ) masked to 0x1f automatically.
*For machines bitmap check is faster than table check, but if the ch variable stored in register than it may perform faster.
Similar Reads
Program to count vowels, consonant, digits and special characters in string.
Given a string and the task is to count vowels, consonant, digits and special character in string. Special character also contains the white space.Examples: Input : str = "geeks for geeks121" Output : Vowels: 5 Consonant: 8 Digit: 3 Special Character: 2 Input : str = " A1 B@ d adc" Output : Vowels:
6 min read
C program to count number of vowels and consonants in a String
Given a string and write a C program to count the number of vowels and consonants in this string. Examples: Input: str = "geeks for geeks" Output: Vowels: 5 Consonants: 8 Input: str = "abcdefghijklmnopqrstuvwxyz" Output: Vowels: 5 Consonants: 21Using For LoopsTake the string as inputTake each charac
4 min read
Convert vowels into upper case character in a given string
Given string str, the task is to convert all the vowels present in the given string to uppercase characters in the given string. Examples: Input: str = âGeeksforGeeksâOutput: GEEksfOrGEEksExplanation:Vowels present in the given string are: {'e', 'o'}Replace 'e' to 'E' and 'o' to 'O'.Therefore, the r
7 min read
Arrange consonants and vowels nodes in a linked list
Given a singly linked list, we need to arrange the consonants and vowel nodes of it in such a way that all the vowel nodes come before the consonants while maintaining the order of their arrival. Examples: Input : a -> b -> c -> e -> d -> o -> x -> i Output : a -> e -> o -
15+ min read
Min. operations required so that no two vowels or consonants are adjacent
Given a string S of size N. In one operation you can replace any character of the string with any other character, the task is to find the minimum number of operations required so that no two vowels or consonants are adjacent. Examples: Input: N = 4, S = 'wlrb'Output: 2Explanation: We can replace w
7 min read
No. of vowels and consonants in a given string in PL/SQL
Prerequisite - PL/SQL introduction In PL/SQL code groups of commands are arranged within a block. A block group related declarations or statements. In declare part, we declare variables and between begin and end part, we perform the operations. Given a string and the task is to find the number of vo
2 min read
Count the number of vowels and consonants in a Linked List
Given a linked list containing lowercase English alphabets, the task is to count the number of consonants and vowels present in the linked list. Example: Input: Linked List: a ->b->o->y -> e ->z->NULLOutput: Vowels : 3Consonants: 3 Input: Linked List: a -> e -> b->c->s-
5 min read
Program to check if first and the last characters of string are equal
We are given a string, we need to check whether the first and last characters of the string str are equal or not. Case sensitivity is to be considered. Examples : Input : university Output : Not Equal Explanation: In the string "university", the first character is 'u' and the last character is 'y',
4 min read
Count of adjacent Vowel Consonant Pairs
Given a string, the task is to count the number of adjacent pairs such that the first element of the pair is a consonant and the second element is a vowel. That is find the number of pairs (i, i+1) such that the ith character of this string is a consonant and the (i+1)th character is a vowel.Example
5 min read
Consonant and Vowel group check in String with Wildcard characters
Given a string S which is composed of lowercase alphabets and wildcard characters i.e. '?'. Here, '?' can be replaced by any of the lowercase alphabets, the task is to return 'True' if there are more than 3 consonants together or more than 5 vowels together else 'False'. Examples: Input: S = aeioup?
5 min read