Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Problem Statement
2.1.
Sample Example 1
2.2.
Sample Example 2
3.
Brute Force Approach
3.1.
Algorithm
3.2.
Implementation in C++
3.2.1.
Time Complexity Analysis
3.2.2.
Space Complexity Analysis
4.
Alternate Approach
4.1.
Algorithm
4.2.
Implementation in C++
4.2.1.
Time Complexity Analysis
4.2.2.
Space Complexity Analysis
5.
Frequently Asked Questions
5.1.
What is the least significant bit (LSB)?
5.2.
What is the right-shift operator, and how does it work?
5.3.
What is space complexity?
6.
Conclusion
Last Updated: Mar 27, 2024
Easy

C++ Program to Check Whether a character is Vowel or Consonant

Author Vidhi Singh
0 upvote

Introduction

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.

Vowels

 

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

  1. 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.
     
  2. We can use either the if statement or the switch case statement for the comparison. The complexities would be the same.
     
  3. If the comparison provides a match, we have a vowel. If not, the given character is a consonant.
     
  4. We will create a function checkVowel(char chk) which will evaluate the type of character.

Implementation in C++

#include<bits/stdc++.h>
using namespace std;


void checkVowel(char chk) 
{
    if (chk == 'a' || chk == 'A' || chk == 'e' ||
        chk == 'E' || chk == 'i' || chk == 'I' ||
        chk == 'o' || chk == 'O' || chk == 'u' || chk == 'U')
        cout << "It is a vowel" << endl;
    else
        cout << "It is a consonant" << endl;
}


int main() 
{
    char inp;
    cout << "The character: ";
    cin >> inp;
    checkVowel(inp);
    return 0;
}
You can also try this code with Online C++ Compiler
Run Code

 

Output:

The character: h
It is a consonant

 

You can try by yourself with the help of online C++ Compiler.

Time Complexity Analysis

There are no loops in this approach, just an if 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)

Also Read - C++ Interview Questions

Alternate Approach

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.

Also readDecimal to Binary c++

The values of the lowercase and uppercase vowels are these:

Hexadecimal and Binary values of the Uppercase and Lowercase vowels


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

  1. We take the given character in variable p. We use the AND operator for p and 0x1f (0b11111) and store it.
     
  2. 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).
     
  3. 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.
     
  4. 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
Run Code

 

Output

The character: u
It is a vowel


Time Complexity Analysis

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++, and Data 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

You can refer to our Guided Path on Coding Ninjas Studio to upskill yourself in Data Structures and AlgorithmsCompetitive ProgrammingJavaScriptSQLSystem Design, and many more!

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!

Do upvote our blog to help other ninjas grow. 

Happy Coding!

Live masterclass