Go 程序检查字符是元音还是辅音

编写一个 Go 程序来检查给定的字符是元音还是辅音。在这里,我们使用 If else 语句来检查字符是否为 a、A、E、e、I、i、o、O、u、U。如果为真,则字符是元音;否则,它是辅音。

package main

import (
    "bufio"
    "fmt"
    "os"
)

func main() {

    reader := bufio.NewReader(os.Stdin)

    fmt.Print("Enter the Starting  Character = ")
    voch, _ := reader.ReadByte()

    if voch == 'a' || voch == 'e' || voch == 'i' || voch == 'o' || voch == 'u' ||
        voch == 'A' || voch == 'E' || voch == 'I' || voch == 'O' || voch == 'U' {
        fmt.Printf("%c is a VOWEL Character\n", voch)
    } else {
        fmt.Printf("%c is a CONSONANT\n", voch)
    }
}
Go Program to Check Character is Vowel or Consonant

Go 程序检查字符是元音还是辅音

在此 Go 示例中,第一个 if 语句(if voch >= ‘A’ && voch <= ‘Z’)用于检查并将大写字符转换为小写。接下来,我们在 if 条件中仅使用小写字符来查找元音和辅音。

package main

import (
    "bufio"
    "fmt"
    "os"
)

func main() {

    reader := bufio.NewReader(os.Stdin)

    fmt.Print("Enter the Starting  Character = ")
    voch, _ := reader.ReadByte()

    if voch >= 'A' && voch <= 'Z' {
        voch = voch + 32
    }

    if voch == 'a' || voch == 'e' || voch == 'i' || voch == 'o' || voch == 'u' {
        fmt.Printf("%c is a VOWEL Character\n", voch)
    } else {
        fmt.Printf("%c is a CONSONANT\n", voch)
    }
}
SureshMac:GoExamples suresh$ go run charVowCon2.go
Enter the Starting  Character = U
u is a VOWEL Character
SureshMac:GoExamples suresh$ go run charVowCon2.go
Enter the Starting  Character = o
o is a VOWEL Character
SureshMac:GoExamples suresh$ go run charVowCon2.go
Enter the Starting  Character = l
l is a CONSONANT

此 Golang 程序使用 ASCII 码来查找字符是元音还是辅音。

package main

import (
    "bufio"
    "fmt"
    "os"
)

func main() {

    reader := bufio.NewReader(os.Stdin)

    fmt.Print("Enter the Starting  Character = ")
    voch, _ := reader.ReadByte()

    if voch == 97 || voch == 101 || voch == 105 || voch == 111 || voch == 117 ||
        voch == 65 || voch == 69 || voch == 73 || voch == 79 || voch == 85 {
        fmt.Printf("%c is a VOWEL Character\n", voch)
    } else if (voch >= 97 && voch <= 122) || (voch >= 65 && voch <= 90) {
        fmt.Printf("%c is a CONSONANT\n", voch)
    } else {
        fmt.Println("Please enter a Valid Character")
    }
}
SureshMac:GoExamples suresh$ go run charVowCon3.go
Enter the Starting  Character = e
e is a VOWEL Character
SureshMac:GoExamples suresh$ go run charVowCon3.go
Enter the Starting  Character = O
O is a VOWEL Character
SureshMac:GoExamples suresh$ go run charVowCon3.go
Enter the Starting  Character = s
s is a CONSONANT