add: questions and exercises for the if statement
This commit is contained in:
39
11-if/exercises/06/solution2/main.go
Normal file
39
11-if/exercises/06/solution2/main.go
Normal file
@@ -0,0 +1,39 @@
|
||||
// For more tutorials: https://blog.learngoprogramming.com
|
||||
//
|
||||
// Copyright © 2018 Inanc Gumus
|
||||
// Learn Go Programming Course
|
||||
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||||
//
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func main() {
|
||||
args := os.Args
|
||||
|
||||
if len(args) != 2 || len(args[1]) != 1 {
|
||||
fmt.Println("Give me a letter")
|
||||
return
|
||||
}
|
||||
|
||||
s := args[1]
|
||||
if strings.IndexAny(s, "aeiou") != -1 {
|
||||
fmt.Printf("%q is a vowel.\n", s)
|
||||
} else if s == "w" || s == "y" {
|
||||
fmt.Printf("%q is sometimes a vowel, sometimes not.\n", s)
|
||||
} else {
|
||||
fmt.Printf("%q is a consonant.\n", s)
|
||||
}
|
||||
|
||||
// Notice that:
|
||||
//
|
||||
// I didn't use IndexAny for the else if above.
|
||||
//
|
||||
// It's because, calling a function is a costly operation.
|
||||
// And, this way, the code is simpler.
|
||||
}
|
Reference in New Issue
Block a user