C++ Program To Find If A Character Is Vowel Or Consonant
Last Updated :
12 Sep, 2023
In English Alphabet, vowels are ‘a’, ‘e’, ‘i’, ‘o’, and ‘u’. The rest of the remaining characters (like ‘b’, ‘c’, ‘d’, ‘f’ ….) are consonants. In this article, we will learn to write a C++ program to check whether a character is Vowel or Consonant.
C++ Program to Check Whether a Character is a Vowel or Consonant
In this method, we check whether the given character matches any of the 5 vowels using if-else statement. If yes, we print “Vowel”, else we print “Consonant”.
C++
#include <iostream>
using namespace std;
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;
}
int main()
{
vowelOrConsonant( 'c' );
vowelOrConsonant( 'E' );
return 0;
}
|
Complexity Analysis
- Time Complexity: O(1)
- Auxiliary Space: O(1)
Check Vowel or Consonant using find() Function
In the below C++ program, the find() function is used to search for the character in the string containing vowel letters. If the character is found, the isVowel function return 1, else it returns 0.
C++
#include <iostream>
#include <string>
using namespace std;
int isVowel( char ch)
{
string str = "aeiouAEIOU" ;
return (str.find(ch) != string::npos);
}
int main()
{
if (isVowel( 'a' ))
cout << "a is vowel" << endl;
else
cout << "a is consonant" << endl;
return 0;
}
|
Complexity Analysis
- Time Complexity: O(1)
- Auxiliary Space: O(1)
Refer to the complete article Program to find if a character is vowel or Consonant for more methods to check if a character is a vowel or consonant.
Share your thoughts in the comments
Please Login to comment...