Go 程序将字符转换为大写

在这个Go程序中,为了将小写字母转换为大写,我们使用IsLetter(如果unicode.IsLetter(lwch))来检查字母。接下来,我们使用unicode.ToUpper函数(up := unicode.ToUpper(lwch))将小写字母转换为大写字母。

package main

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

func main() {

    reader := bufio.NewReader(os.Stdin)

    fmt.Print("Enter Character to Convert into Uppercase = ")
    lwch, _, _ := reader.ReadRune()

    if unicode.IsLetter(lwch) {
        up := unicode.ToUpper(lwch)
        fmt.Printf("The Uppercase Character of %c = %c\n", lwch, up)
    } else {
        fmt.Printf("Please Enter a Valid Alphabet\n")
    }
}
Go Program to Convert Character to Uppercase

Go 程序将小写字母转换为大写

在这个小写转大写转换示例中,我们将用户输入的字节字符转换为Rune(unicode.ToUpper(rune(lwch))),然后使用ToUpper函数。

package main

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

func main() {

    reader := bufio.NewReader(os.Stdin)

    fmt.Print("Enter Character to Convert into Uppercase = ")
    lwch, _ := reader.ReadByte()

    if unicode.IsLetter(rune(lwch)) {
        up := unicode.ToUpper(rune(lwch))
        fmt.Printf("The Uppercase Character of %c = %c\n", lwch, up)
    } else {
        fmt.Printf("Please Enter a Valid Alphabet\n")
    }
}
SureshMac:GoExamples suresh$ go run charToUpper2.go
Enter Character to Convert into Uppercase = q
The Uppercase Character of q = Q
SureshMac:GoExamples suresh$ go run charToUpper2.go
Enter Character to Convert into Uppercase = 9
Please Enter a Valid Alphabet

这个Golang将字符转换为大写的程序使用ASCII码(如果lwch >= 97 && lwch <= 122)来查找小写字母。如果为真,我们将ASCII值减去32(up := lwch – 32)将小写字母转换为大写。

package main

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

func main() {

    reader := bufio.NewReader(os.Stdin)

    fmt.Print("Enter Character to Convert into Uppercase = ")
    lwch, _ := reader.ReadByte()

    if lwch >= 97 && lwch <= 122 {
        up := lwch - 32
        fmt.Printf("The Uppercase Character of %c = %c\n", lwch, up)
    } else {
        fmt.Printf("Either You entered the Uppercase Char or Inalid Alphabet\n")
    }
}
SureshMac:GoExamples suresh$ go run charToUpper3.go
Enter Character to Convert into Uppercase = k
The Uppercase Character of k = K
SureshMac:GoExamples suresh$ go run charToUpper3.go
Enter Character to Convert into Uppercase = M
Either You entered the Uppercase Char or Inalid Alphabet

在这个示例中,我们使用a和z来查找小写字母。如果为真,我们从中减去32的ASCII值。

package main

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

func main() {

    reader := bufio.NewReader(os.Stdin)

    fmt.Print("Enter Character to Convert into Uppercase = ")
    lwch, _ := reader.ReadByte()

    if lwch >= 'a' && lwch <= 'z' {
        up := lwch - 32
        fmt.Printf("The Uppercase Character of %c = %c\n", lwch, up)
    } else {
        fmt.Printf("Either You entered the Uppercase Char or Inalid Alphabet\n")
    }
}
SureshMac:GoExamples suresh$ go run charToUpper4.go
Enter Character to Convert into Uppercase = s
The Uppercase Character of s = S
SureshMac:GoExamples suresh$ go run charToUpper4.go
Enter Character to Convert into Uppercase = S
Either You entered the Uppercase Char or Inalid Alphabet