programming

How to Determine Consonant Vowels in Golang

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

package main  
  
import (  
 "fmt"  
)  
  
func isVokal(character rune) {  
 if character == 'a' || character == 'e' || character == 'i' || character == 'o' || character == 'u' {  
  fmt.Printf(" %c adalah vokal\n", character)  
 } else {  
  fmt.Printf(" %c adalah konsonan\n", character)  
 }  
  
}  
func main() {  
 isVowel('a') // vokal 
 isVowel('b') // konsonan  
} 

The result is below

a adalah vokal  
b adalah konsonan

The program determines vowels and consonants using switch case

package main  
  
import (  
 "fmt"  
)  
  
func isVokal(character rune) {  
 switch character {  
 case 'a', 'e', 'i', 'o', 'u':  
  fmt.Printf(" %c adalah vokal\n", character)  
 default:  
  fmt.Printf(" %c adalah konsonan\n", character)  
 }  
}  
func main() {  
 isVowel('e') // vokal
 isVowel('g') // konsonan  
}  

The result is below

e adalah vokal
g 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

package main  
  
import (  
 "fmt"  
)  
  
func main() {  
 str := "santekno"  
 count := 0  
 for _, ch := range str {  
  switch ch {  
  case 'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U':  
   count++  
  }  
 }  
 fmt.Printf("kalimat %s mengandung vokal sebanyak: %d\n", str, count)  
  
}  

The result is below

kalimat santeno mengandung vokal sebanyak: 3
comments powered by Disqus