编写一个 Go 程序来检查字符是大写还是小写。unicode.IsUpper 函数用于查找 Rune 是否为大写字符。我们在 If else 条件(if unicode.IsUpper(upch))中使用此 IsUpper 函数来查找给定的字符是否为大写。
package main
import (
"bufio"
"fmt"
"os"
"unicode"
)
func main() {
reader := bufio.NewReader(os.Stdin)
fmt.Print("Enter Character to Check Uppercase = ")
upch, _, _ := reader.ReadRune()
if unicode.IsUpper(upch) {
fmt.Println("You have Entered the Uppercase Character")
} else {
fmt.Println("The entered Character is Not the Uppercase")
}
}

Go 程序检查字符是大写还是小写
在此大写字母示例中,我们将给定的字节转换为 Rune(if unicode.IsUpper(rune(upch))),然后使用 IsUpper 函数。
package main
import (
"bufio"
"fmt"
"os"
"unicode"
)
func main() {
reader := bufio.NewReader(os.Stdin)
fmt.Print("Enter Character to Check Uppercase = ")
upch, _ := reader.ReadByte()
if unicode.IsUpper(rune(upch)) {
fmt.Println("You have Entered the Uppercase Character")
} else {
fmt.Println("The entered Character is Not the Uppercase")
}
}
SureshMac:GoExamples suresh$ go run charIsUpper2.go
Enter Character to Check Uppercase = o
The entered Character is Not the Uppercase
SureshMac:GoExamples suresh$ go run charIsUpper2.go
Enter Character to Check Uppercase = M
You have Entered the Uppercase Character
在此 Golang 示例中,我们使用 If 条件(if upch >= ‘A’ && upch <= ‘Z’)来检查字符是否在 A 和 Z 之间,如果为真,则表示大写。
package main
import (
"bufio"
"fmt"
"os"
)
func main() {
reader := bufio.NewReader(os.Stdin)
fmt.Print("Enter Character to Check Uppercase = ")
upch, _ := reader.ReadByte()
if upch >= 'A' && upch <= 'Z' {
fmt.Println("You have Entered the Uppercase Character")
} else {
fmt.Println("The entered Character is Not the Uppercase")
}
}
SureshMac:GoExamples suresh$ go run charIsUpper3.go
Enter Character to Check Uppercase = A
You have Entered the Uppercase Character
SureshMac:GoExamples suresh$ go run charIsUpper3.go
Enter Character to Check Uppercase = k
The entered Character is Not the Uppercase
此 程序 使用 ASCII 码(if upch >= 97 && upch <= 122)来查找字符是否为大写。
package main
import (
"bufio"
"fmt"
"os"
)
func main() {
reader := bufio.NewReader(os.Stdin)
fmt.Print("Enter Character to Check Uppercase = ")
upch, _ := reader.ReadByte()
if upch >= 97 && upch <= 122 {
fmt.Println("You have Entered the Lowercase Character")
} else {
fmt.Println("The entered Character is the Uppercase")
}
}
SureshMac:GoExamples suresh$ go run charIsUpper4.go
Enter Character to Check Uppercase = B
The entered Character is the Uppercase
SureshMac:GoExamples suresh$ go run charIsUpper4.go
Enter Character to Check Uppercase = z
You have Entered the Lowercase Character