Goku
Goku

Reputation: 1840

check for vowels or numbers in a string using golang

I want know a best way to check if some char is vowel, and find a pattern to know how check for numbers too, this is my first attempt...http://play.golang.org/p/wI8pYV3BqO

package main

import "fmt"

type groupChars []rune

func isVowel(c rune) bool {
    vowels := groupChars{'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'}
    for _, value := range vowels {
        if value == c {
            return true
        }
    }
    return false
}

func main() {
    myString := "OLapOKA3EOR"
    t := 0
    for _, value := range myString {
        if isVowel(value) {
            fmt.Printf("%c is Vowel\n", value)
            t++
        }
    }
    fmt.Printf("%d Vowels.", t)

}

Thanks

Upvotes: 0

Views: 7475

Answers (1)

Eve Freeman
Eve Freeman

Reputation: 33145

I think you want something like this:

for _, value := range myString {
    switch value {
    case 'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U':
        t++
    }
}

http://play.golang.org/p/MFboCiikYW

For numbers you could do:

for _, value := range myString {
    switch {
    case value >= '0' && value <= '9':
        t++
    }
}

http://play.golang.org/p/I4CbuHEVVH

Upvotes: 10

Related Questions