Vowels are alphabets that produce an uninterrupted sound. There are five vowels in the entire alphabet (i.e. a, e, i, o, u), while the rest are consonants.
Let’s discuss how to find if a given character is a vowel or consonant in C++.
Problem Statement
Given an alphabet, output if it is a vowel or a consonant. The alphabet will be given in uppercase or lowercase.
Sample Example 1
Input: b
Output: consonant
Sample Example 2
Input: u
Output: vowel
Brute Force Approach
This approach is very simple to implement. We compare the character to all the vowels, lowercase and uppercase, and display vowels if it matches. Else the character is considered as a consonant.
Algorithm
This approach is quite straightforward. Take the given character and compare it with the lowercase and uppercase versions of all the vowels, which include a, e, i, o, u.
We can use either the if statement or the switch case statement for the comparison. The complexities would be the same.
If the comparison provides a match, we have a vowel. If not, the given character is a consonant.
We will create a function checkVowel(char chk) which will evaluate the type of character.
This approach is trickier one. But, the point of discussing it is that it deals with the Hexadecimal and Binary values of the vowels. This approach has been mentioned just for the sake of reference. However, we do not recommend this approach to be followed.
The values of the lowercase and uppercase vowels are these:
As we can see, the lowest significant bit (LSB) is the same for all the vowels. So, we can find a number (0x208222) that would give 1 as LSB after a right-shift of 1, 5, 19, 15, confirming that the number is a vowel. Else, it would be considered a consonant.
Algorithm
We take the given character in variable p. We use the AND operator for p and 0x1f (0b11111) and store it.
We have chosen a specific number that would always have LSB 1 when it is right-shifted by certain values. These values are the result of the AND operator between p and 0x1f when p is a vowel. The specific number comes to be 0x208222 (0b1000001000001000100010).
0x208222 is right-shifted by the value of the result of the AND operation for p and 0x1f. The result is then again operated by the AND operator along with 1 to check if the LSB is still 1.
If the LSB is still 1, the given character is a vowel. Else, it is a consonant.
Implementation in C++
#include<bits/stdc++.h>
using namespace std;
// will return 1 if p is vowel else 0
int checkVowel(char p)
{
return (0x208222 >> (p & 0x1f)) & 1;
}
int main()
{
char inp;
cout << "The character: ";
cin >> inp;
if(checkVowel(inp) == 1)
cout << "It is a vowel" << endl;
else
cout << "It is a consonant" << endl;
return 0;
}
You can also try this code with Online C++ Compiler
In this approach, there are no loops, just an if-else statement, so it will only run once. Therefore it is O (1).
Space Complexity Analysis
In this approach, constant extra space is needed, so the space complexity is O(1)
Frequently Asked Questions
What is the least significant bit (LSB)?
In a binary number, the rightmost bit is called the least significant bit (LSB).
What is the right-shift operator, and how does it work?
“>>” is the right-shift operator. This operator basically shifts the bits of the first operand over by the number of positions indicated by the second operand.
For example, 11010011 >> 2 becomes 00110100.
What is space complexity?
The total memory space used by the algorithm, including the space of input values for execution, is referred to as space complexity.
Conclusion
This article extensively discusses the programming problem: C++ Program to Check Whether a character is a vowel or a consonant along with their pseudocode, implementation, and time trade-offs.
We recommend checking out more blogs related to C++ like Introduction to C++, Difference between C and C++, andData Types in C++. We hope that this blog has helped you enhance your knowledge regarding C++ Program to Check Whether a Character is a Vowel or a Consonant, and if you would like to learn more, check out our articles on Coding Ninjas Blogs.
If you want to test your competency in coding, you may check out the Mock Test Series and participate in the Contests organized on Coding Ninjas Studio! But if you have just started your learning process and are looking for questions asked by tech giants like Amazon, Microsoft, Uber, etc; you must look at the Problems,Interview Experiences, and Interview Bundle for placement preparations.
Nevertheless, you may consider our Courses to give your career an edge over others!