Methods to check whether the character is a vowel or not in c++
In this article, we discuss how to check whether a given character is a vowel or not in c++. The methods that are discussed are given below.
Let’s discuss these methods one by one.
Method 1-Using if-else statement
In this method, we will use the if-else statement to check whether a given character is a vowel or not. Let’s write the code for this.
#include <iostream>
using namespace std;
int main() {
char ch='u';
if(ch=='a' || ch=='e' || ch=='i' || ch=='o' || ch=='u')
{
cout<<"Character "<<ch<<" is a vowel";
}
else
{
cout<<"Character "<<ch<<" is not a vowel";
}
return 0;
}
Output
Character u is a vowel
Note: Here we are writing code for lower case letters. We can use the same logic for upper case letters also.
Method 2-Using if-else if-else statement
In this method, we use the if-else if-else statement to check whether a given character is a vowel or not. Let’s write the code for this.
#include <iostream>
using namespace std;
int main() {
char ch='u';
if(ch=='a')
{
cout<<"Character "<<ch<<" is a vowel";
}
else if(ch=='e')
{
cout<<"Character "<<ch<<" is a vowel";
}
else if(ch=='i')
{
cout<<"Character "<<ch<<" is a vowel";
}
else if(ch=='o')
{
cout<<"Character "<<ch<<" is a vowel";
}
else if(ch=='u')
{
cout<<"Character "<<ch<<" is a vowel";
}
else
{
cout<<"Character "<<ch<<" is not a vowel";
}
return 0;
}
Output
Character u is a vowel
Method 3-Using switch statement
In this method, we use the switch statement to check whether a given character is a vowel or not. Let’s write the code for this.
#include <iostream>
using namespace std;
int main() {
char ch='h';
switch(ch)
{
case 'a':
cout<<"Character "<<ch<<" is a vowel";
break;
case 'e':
cout<<"Character "<<ch<<" is a vowel";
break;
case 'i':
cout<<"Character "<<ch<<" is a vowel";
break;
case 'o':
cout<<"Character "<<ch<<" is a vowel";
break;
case 'u':
cout<<"Character "<<ch<<" is a vowel";
break;
default:
cout<<"Character "<<ch<<" is not a vowel";
}
return 0;
}
Output
Character h is not a vowel
So these are the methods to check whether a given character is a vowel or not.