Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Golang Program to Check Whether an Alphabet is Vowel or Consonant
In this tutorial we will write a go language code to check whether an alphabet is vowel or consonant.
Difference between Vowel and Consonant
A character is called vowel if it is in any one of the following list of characters {a, e, I, o, u}.
All the remaining characters are called consonants. In English language there are 5 vowels and 21 consonants.
Here we are going to use following 2 methods ?
Method 1: Using if/else statement
In this method we will use the conditional statements to check if a character is a vowel or consonant.
Method 2: Using a switch case
In this method we will use switch to check if an alphabet is a vowel or consonant.
Example 1: Golang Program to check if a given character is Vowel or Consonant
Syntax
if condition {
//code
} else {
//code
}
If conditionals are used to check a particular condition and run the lines of code only if the condition is satisfied.
We can check multiple conditions using if-else conditionals that will run one set of code if first condition is satisfied and second set of codes if second condition is satisfied.
Algorithm
Step 1 ? Import the package fmt.
Step 2 ? Initialize and define the isVovel() function.
Step 3 ? start the main() function.
Step 4 ? Initialize the variables of data type string and store a character in it.
Step 5 ? Call the isVovel() function.
Step 6 ? Store its results in a variable.
Step 7 ? Print the results on the screen.
Example
<div class="execute"></div><div class="code-mirror language-java" contenteditable="plaintext-only" spellcheck="false" style="outline: none; overflow-wrap: break-word; overflow-y: auto; white-space: pre-wrap;"><span class="token comment">//GOLANG PROGRAM TO CHECK IF AN ALPHABET IS A VOWEL OR CONSONANT</span>
<span class="token keyword">package</span> <span class="token namespace">main</span>
<span class="token comment">// fmt package provides the function to print anything</span>
<span class="token keyword">import</span> <span class="token string">"fmt"</span>
<span class="token comment">// initialize and define a isVowel() function</span>
func <span class="token function">isVowel</span><span class="token punctuation">(</span>character rune<span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token comment">// conditional statements to check if a character is vowel</span>
<span class="token keyword">if</span> character <span class="token operator">==</span> <span class="token string">'a'</span> <span class="token operator">||</span> character <span class="token operator">==</span> <span class="token string">'e'</span> <span class="token operator">||</span> character <span class="token operator">==</span> <span class="token string">'i'</span> <span class="token operator">||</span> character <span class="token operator">==</span> <span class="token string">'o'</span> <span class="token operator">||</span> character <span class="token operator">==</span> <span class="token string">'u'</span> <span class="token punctuation">{</span>
<span class="token comment">// printing the vowels</span>
<span class="token class-name"><span class="token namespace">fmt<span class="token punctuation">.</span></span>Printf</span><span class="token punctuation">(</span><span class="token string">"Input Character = %c \n It is a vowel\n"</span><span class="token punctuation">,</span> character<span class="token punctuation">)</span>
<span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
<span class="token comment">// print a consonent</span>
<span class="token class-name"><span class="token namespace">fmt<span class="token punctuation">.</span></span>Printf</span><span class="token punctuation">(</span><span class="token string">"Input character = %c \n It is a consonent \n"</span><span class="token punctuation">,</span> character<span class="token punctuation">)</span>
<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
<span class="token comment">// starting the main function</span>
func <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token comment">// calling the isVowel() function and passing character to it as arguments.</span>
<span class="token function">isVowel</span><span class="token punctuation">(</span><span class="token string">'e'</span><span class="token punctuation">)</span> <span class="token comment">// vowel</span>
<span class="token function">isVowel</span><span class="token punctuation">(</span><span class="token string">'b'</span><span class="token punctuation">)</span> <span class="token comment">// consonant</span>
<span class="token punctuation">}</span>
</div><div class="output-wrapper"><div class="console-close"></div><div class="code-output"></div></div>
Output
Input Character = a It is a vowel Input character = b It is a consonent
Description of the Code
1. First, we have to import the fmt package that allows us to print anything on the screen.
2. Then we have defined the isVowel() function, that will contain our logic to check if a character is a vowel or consonant.
3. This function contains if-else conditionals to compare the characters and then prints the respective result on the screen.
4. Start the main() function.
5. Call the isVowel() function by passing a particular character as argument to it.
6. Check the character against the vowels if the character provided is a vowel then print on the screen that given character is a vowel otherwise print the given character is a consonant.
Example 2: Golang Program to check if the alphabet is Vowel or Consonant using a Switch Case
Syntax
switch optstatement; optexpression{
case expression1: Statement.
case expression2: Statement.
...
default: Statement.
}
Algorithm
Step 1 ? Import the package fmt.
Step 2 ? Initialize and define the isVovel() function.
Step 3 ? Use the switch case to print vowels and consonants.
Step 4 ? start the main() function.
Step 5 ? Call the isVovel() function.
Step 6 ? print the results on the screen using fmt.Printf() function.
Example
<div class="execute"></div><div class="code-mirror language-java" contenteditable="plaintext-only" spellcheck="false" style="outline: none; overflow-wrap: break-word; overflow-y: auto; white-space: pre-wrap;"><span class="token comment">// GOLANG PROGRAM TO CHECK THE ALPHABET IS VOWEL OR CONSONANT USING SWITCH</span>
<span class="token keyword">package</span> <span class="token namespace">main</span>
<span class="token comment">// fmt package provides the function to print anything</span>
<span class="token keyword">import</span> <span class="token string">"fmt"</span>
<span class="token comment">// defining the function with a parameter of rune</span>
func <span class="token function">isVowel</span><span class="token punctuation">(</span>character rune<span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token comment">// Using the switch statement</span>
<span class="token keyword">switch</span> character <span class="token punctuation">{</span>
<span class="token comment">// defining the switch casses</span>
<span class="token keyword">case</span> <span class="token string">'a'</span><span class="token punctuation">,</span> <span class="token string">'e'</span><span class="token punctuation">,</span> <span class="token string">'i'</span><span class="token punctuation">,</span> <span class="token string">'o'</span><span class="token punctuation">,</span> <span class="token string">'u'</span><span class="token operator">:</span>
<span class="token comment">// printing the output</span>
<span class="token class-name"><span class="token namespace">fmt<span class="token punctuation">.</span></span>Printf</span><span class="token punctuation">(</span><span class="token string">"The provided character %c is a vowel\n"</span><span class="token punctuation">,</span> character<span class="token punctuation">)</span>
<span class="token comment">// defining the default case</span>
<span class="token keyword">default</span><span class="token operator">:</span>
<span class="token class-name"><span class="token namespace">fmt<span class="token punctuation">.</span></span>Printf</span><span class="token punctuation">(</span><span class="token string">"The provided character %c is a consonant\n"</span><span class="token punctuation">,</span> character<span class="token punctuation">)</span>
<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
<span class="token comment">// main fuction</span>
func <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token comment">//call the isVowel() function</span>
<span class="token function">isVowel</span><span class="token punctuation">(</span><span class="token string">'n'</span><span class="token punctuation">)</span>
<span class="token function">isVowel</span><span class="token punctuation">(</span><span class="token string">'o'</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>
</div><div class="output-wrapper"><div class="console-close"></div><div class="code-output"></div></div>
Output
The provided character n is a consonant The provided character o is a vowel
Description of the Code
1. First, we have to import the fmt package that allows us to print anything on the screen.
2. Then we have defined the isVowel() function, that will contain our logic to check if a character is a vowel or consonant.
3. This function contains switch case to compare the characters and then prints the respective result on the screen.
4. The first case is one in which one set of code is executed if any one of the vowels is passed as the argument in the function and respective message that the provided character is a vowel is printed on the screen.
5. Then we need to define the default case. If a character is passed that is not a vowel then default case executes and the result is printed on the screen that the provided character is a consonant.
6. Start the main() function.
7. Call the isVowel() function by passing a particular character as argument to it.
8. Check the character against the vowels if the character provided is a vowel then print on the screen that given character is a vowel otherwise print the given character is a consonant.
Conclusion
In the above two examples we have successfully compiled and executed the Golang program to check if the alphabets are vowels or consonants. We have used 2 different approaches in this program.
In the first method we have used conditional statements, and compared the alphabet with the vowels {a, e, i, o, u}
In the second method we have used switch case to check if a given character is vowel or consonant.
