英文:
check for vowels or numbers in a string using golang
问题
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)
}
英文:
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
答案1
得分: 7
我认为你想要这样的代码:
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 _, value := range myString {
switch {
case value >= '0' && value <= '9':
t++
}
}
http://play.golang.org/p/I4CbuHEVVH
英文:
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++
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论