How to Check whether alphabet is Vowel or consonant in Golang

In this example, We will learn how to check whether the alphabet is vowel or consonant using [if else](/2018/11/learn-golang-tutorials-if-else.html) and [switch case](/https://www.2018/11/learn-golang-tutorials-switch-case.html) and Count of vowels in a String with an example.

the vowel is called for a character if the Character contains any of the a, e, i, o, or u characters. consonant is a character that is not a vowels character.

Please have a look at the below golang features before understanding these programs.

How to check the alphabet is Vowel or consonant using the golang if-else statement

In this program, we will check given character is a vowel or consonant using the [if else](/2018/11/learn-golang-tutorials-if-else.html) block of code.

package main

import (
    "fmt"
)

func isVowel(character rune) {
    if character == 'a' || character == 'e' || character == 'i' || character == 'o' || character == 'u' {
        fmt.Printf(" %c is vowel\n", character)
    } else {
        fmt.Printf(" %c is consonant\n", character)
    }

}
func main() {
    isVowel('a') // vowel
    isVowel('b') // consonant
}

Output:

a is vowel
b is consonant

In the above example,

  • Character is stored as [rune type]/2018/11/learn-golang-tutorials-rune-types.html.
  • Written isVowel method with [rune](/2018/11/learn-golang-tutorials-rune-types.html) type argument
  • It checks for a given character contains anyone (a,e, i,o,u) using an if-else statement and returns a vowel
  • else return consonant character

How to check alphabet is Vowel or consonant using a Golang switch case

It is another way of checking a given character is vowel using [switch case](/https://www.2018/11/learn-golang-tutorials-switch-case.html).

package main

import (
    "fmt"
)

func isVowel(character rune) {
    switch character {
        case 'a', 'e', 'i', 'o', 'u':
            fmt.Printf(" %c is vowel\n", character)
        default:
            fmt.Printf(" %c is consonant\n", character)
    }
}
func main() {
    isVowel('e') // vowel
    isVowel('g') // consonant
}

Output:

e is vowel
g is consonant

Instead of an if-else statement, Switch case is used. Switch case evaluates the expression and matched case is executed. if the character is matched with any of the characters (a,e, i,o,u), the case is printed with a vowel to the console. default case consonant is printed to console

How to find vowels count in a given String in the Go language

This program checks the given string and returns the vowels count presented in a string.

package main

import (
    "fmt"
)

func main() {
    str: = "kiran"
    count: = 0
    for _,
    ch: = range str {
        switch ch {
            case 'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U':
                count++
        }
    }
    fmt.Printf(" %s string contains vowels count: %d\n", str, count)

}

Output:

kiran string contains vowels count: 2

Conclusion

In this post, You will learn the following things in a go programming language

  • Check given character is a vowel or consonant using if-else and switch expression
  • How to find vowels count in a given string.