Skip to content
Santekno.com | Level Up Your Engineering Skills
EN
📖 0%
15 Apr 2021 · 2 min read ·Article 10 / 119
Go

How to Determine Consonant Vowels in Golang

IH
Ihsan Arif
Writer at Santekno · Backend Engineer

Introduction

Determining consonant vowels here will be divided into several examples. Later we will know better which processes will be carried out sequentially. What we already know is that the vowel characters are a,i,u,e,o and this will be a condition in the program later.

The program determines vowels and consonants using if..else

go
 1package main  
 2  
 3import (  
 4 "fmt"  
 5)  
 6  
 7func isVokal(character rune) {  
 8 if character == 'a' || character == 'e' || character == 'i' || character == 'o' || character == 'u' {  
 9  fmt.Printf(" %c adalah vokal\n", character)  
10 } else {  
11  fmt.Printf(" %c adalah konsonan\n", character)  
12 }  
13  
14}  
15func main() {  
16 isVowel('a') // vokal 
17 isVowel('b') // konsonan  
18} 

The result is below

bash
1a adalah vokal  
2b adalah konsonan

The program determines vowels and consonants using switch case

go
 1package main  
 2  
 3import (  
 4 "fmt"  
 5)  
 6  
 7func isVokal(character rune) {  
 8 switch character {  
 9 case 'a', 'e', 'i', 'o', 'u':  
10  fmt.Printf(" %c adalah vokal\n", character)  
11 default:  
12  fmt.Printf(" %c adalah konsonan\n", character)  
13 }  
14}  
15func main() {  
16 isVowel('e') // vokal
17 isVowel('g') // konsonan  
18}  
The result is below
bash
1e adalah vokal
2g adalah konsonan

Explanation

In this program, the user is asked to enter the characters stored in the variable c. Then, this character is checked to see if it is one of these ten characters, namely A, a, I, i, U, u, E, e, O and o using the logical OR operator ||. If one of the ten characters in the alphabet is vowel then that alphabet is a consonant.

(Bonus) The program counts vowels in sentences

go
 1package main  
 2  
 3import (  
 4 "fmt"  
 5)  
 6  
 7func main() {  
 8 str := "santekno"  
 9 count := 0  
10 for _, ch := range str {  
11  switch ch {  
12  case 'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U':  
13   count++  
14  }  
15 }  
16 fmt.Printf("kalimat %s mengandung vokal sebanyak: %d\n", str, count)  
17  
18}  

The result is below

bash
1kalimat santeno mengandung vokal sebanyak: 3

Related Articles

💬 Comments