In this post, you’ll learn how to Check if a Character is a Vowel or Consonant in C++.
In this lesson, you will learn how to Check if a Character is a Vowel or Consonant, with decision making statements, logical operator (OR) and relational operator using the C++ language. Let’s look at the below source code.
How to check is a Character is a Vowel or Consonant?
RUN CODE SNIPPETSource Code
#include <iostream> using namespace std; int main() { char c; cin >> c; cout << "Enter a Character: "<< c <<endl; if (c == 'a' || c == 'e' || c =='i' || c=='o' || c=='u' || c=='A' || c=='E' || c=='I' || c=='O' || c=='U') cout <<"\n"<< c << " is VOWEL"; else cout <<"\n"<< c <<" is CONSONANT"; return 0; }
Input
c
Output
Enter a Character c is CONSONANT
#include <iostream>
- This line which is called the header file.Â
#include
statement tells the compiler to use available files and<iostream>
is the name of the specific that we have used in this code. The<iostream>
file stands for Input and Output statement.
using namespace std;
- The C++ has a standard library that has files for different functions and this line is used to access the standard file for input and output statements.
int main();
- This line usually controls the function of the code, as it calls the functions to perform their tasks.
- The
int main()
shows that the input value is a type of integer, once the program is executed the function returns to the main function, by using the statement ‘return 0;’.
{ }
- The opening ‘ { ‘ and the closing ‘ } ‘ curly braces mark the start and the finish of the main function
- Every statement and value between these braces belong to the main function.
The above statements are the main factors that support the function of the source code. Now we can look into the working and layout of the code’s function.
- To start, we declare
char c
to store the character, thecout
function is used to display the output statement andcin >> c
stores the value entered by the user. - Using the relational operator
(c == a)
and logical operator (OR)||
, we give declare the condition(c == a) || (c == A)
,using the Vowels (both capital and small). - Then include them in the decision making
if else
statement. - The OR function sees if the first condition is satisfied, if not it moves to the next condition. When none of the conditions is satisfied in the
if
statement, then theelse
statement output is displayed. - If the condition is satisfied the character is a Vowel, else the character is a Consonant.
- Using the statement
cout<< c
we display the character, along with the output statement. - End the function with
return 0;
this returns the function to main( ).
Note: The ‘ << endl ‘ in the code is used to end the current line and move to the next line and ‘\n’ is also a new line function, to understand how both the functions work exclude it from the code, move it around and work with it.
Additional Source Code
You can also try the source code given below, the following code is structured to find whether the Character is a Vowel or a Consonant along with an additional condition.
RUN CODE SNIPPET#include <iostream> using namespace std; int main() { char c; cin >> c; cout << "Enter a Character: "<< c << endl; if (!isalpha(c)) cout<<" \nEntered character '"<< c <<"' is a special character"; else if (c == 'a' || c == 'e' || c =='i' || c=='o' || c=='u' || c=='A' || c=='E' || c=='I' || c=='O' || c=='U') cout <<"\n"<< c << " is VOWEL"; else cout <<"\n"<< c <<" is CONSONANT"; return 0; }
Input
+
Output
Enter a Character: + Entered character '+' is a special character
- In this source code we use an additional function
(!isalpha(c))
, this function checks whether the character is an alphabet or a special character. - The first
if else
statement is executed and the output is displayed, if the first condition is satisfied. - If the character is an alphabet then, the condition is not satisfied and it moves to the next
if else
statement. This is a nested if else statement, that is anif else
statement has anotherif else
statement in it. - The rest is similar to the previous source code to find whether the Character is a Vowel or Consonant.