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

//GOLANG PROGRAM TO CHECK IF AN ALPHABET IS A VOWEL OR CONSONANT package main // fmt package provides the function to print anything import "fmt" // initialize and define a isVowel() function func isVowel(character rune) { // conditional statements to check if a character is vowel if character == 'a' || character == 'e' || character == 'i' || character == 'o' || character == 'u' { // printing the vowels fmt.Printf("Input Character = %c \n It is a vowel\n", character) } else { // print a consonent fmt.Printf("Input character = %c \n It is a consonent \n", character) } } // starting the main function func main() { // calling the isVowel() function and passing character to it as arguments. isVowel('e') // vowel isVowel('b') // consonant }

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

// GOLANG PROGRAM TO CHECK THE ALPHABET IS VOWEL OR CONSONANT USING SWITCH package main // fmt package provides the function to print anything import "fmt" // defining the function with a parameter of rune func isVowel(character rune) { // Using the switch statement switch character { // defining the switch casses case 'a', 'e', 'i', 'o', 'u': // printing the output fmt.Printf("The provided character %c is a vowel\n", character) // defining the default case default: fmt.Printf("The provided character %c is a consonant\n", character) } } // main fuction func main() { //call the isVowel() function isVowel('n') isVowel('o') }

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.

Updated on: 04-Apr-2023

506 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements