How to Check whether alphabet is Vowel or consonant in Golang
- Admin
- Mar 10, 2024
- Golang-examples
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.
A vowel
is identified in a character if it contains any of the characters a, e, i, o, or u. A consonant
is a character that is not a vowel
character.
Please have a look at the below golang features before understanding these programs.
How to check if an alphabet is a vowel or consonant using the Golang if-else statement.
In this program, we will check if a given character is a vowel or consonant using the [if else](/2018/11/learn-golang-tutorials-if-else.html)
block of code.
In this program, we will check given character is a vowel
or consonant
using the 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,
In the above example:
- The character is stored as a
[rune type]/2018/11/learn-golang-tutorials-rune-types.html
. - A method
isVowel
is implemented with arune
type argument. - It checks if the given character contains any of the vowels (a, e, I, o, u) using an if-else statement and returns
vowel
. Otherwise, it returns the character as aconsonant
.
How to check alphabet is Vowel or consonant using a Golang switch case
It is another way of checking whether a given character is a 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, a Switch
case is used. The Switch case
evaluates the expression, and the matched case is executed. If the character matches any of the characters (a, e, i, o, u), the corresponding case prints vowel
to the console. In the default case, consonant
is printed to the console.
How to find vowels count in a given String in the Go language
This program checks the given string and returns the count of vowels
present in the 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
- Checking whether a given character is a vowel or consonant using if-else and switch expressions
- Determining the count of vowels in a given string.