In this Go Program, to convert the character to lowercase, we used IsLetter to check the character is an alphabet. Next, we used the unicode ToLower function (lw := unicode.ToLower(upch)) that converts the uppercase characters to lowercase.
package main import ( "bufio" "fmt" "os" "unicode" ) func main() { reader := bufio.NewReader(os.Stdin) fmt.Print("Enter Character to Convert into Lowercase = ") upch, _, _ := reader.ReadRune() if unicode.IsLetter(upch) { lw := unicode.ToLower(upch) fmt.Printf("The Lowercase Character of %c = %c\n", upch, lw) } else { fmt.Printf("Please Enter a Valid Alphabet\n") } }
Go Program to Convert Uppercase Character to Lowercase
In this Go uppercase to lowercase convert example, we cast the given byte character to Rune (lw := unicode.ToLower(rune(upch))) and then use the ToLower function.
package main import ( "bufio" "fmt" "os" "unicode" ) func main() { reader := bufio.NewReader(os.Stdin) fmt.Print("Enter Character to Convert into Lowercase = ") upch, _ := reader.ReadByte() if unicode.IsLetter(rune(upch)) { lw := unicode.ToLower(rune(upch)) fmt.Printf("The Lowercase Character of %c = %c\n", upch, lw) } else { fmt.Printf("Please Enter a Valid Alphabet\n") } }
SureshMac:GoExamples suresh$ go run charToLower2.go
Enter Character to Convert into Lowercase = D
The Lowercase Character of D = d
SureshMac:GoExamples suresh$ go run charToLower2.go
Enter Character to Convert into Lowercase = 1
Please Enter a Valid Alphabet
This Golang Convert Character to an Uppercase Program uses the ASCII codes (if upch >= 65 && upch <= 90) to find the character is uppercase. If true, we added 32 (lw := upch + 32) to the ASCII value to convert uppercase to the lowercase character.
package main import ( "bufio" "fmt" "os" ) func main() { reader := bufio.NewReader(os.Stdin) fmt.Print("Enter Character to Convert into Lowercase = ") upch, _ := reader.ReadByte() if upch >= 65 && upch <= 90 { lw := upch + 32 fmt.Printf("The Lowercase Character of %c = %c\n", upch, lw) } else { fmt.Printf("Either You entered the Lowercase Char or Inalid Alphabet\n") } }
SureshMac:GoExamples suresh$ go run charToLower3.go
Enter Character to Convert into Lowercase = q
Either You entered the Lowercase Char or Inalid Alphabet
SureshMac:GoExamples suresh$ go run charToLower3.go
Enter Character to Convert into Lowercase = B
The Lowercase Character of B = b
In this Golang example, We used the A and Z within the If condition (if upch >= ‘A’ && upch <= ‘Z’) to find the uppercase character. If True, we are adding 32 to its ASCII value.
package main import ( "bufio" "fmt" "os" ) func main() { reader := bufio.NewReader(os.Stdin) fmt.Print("Enter Character to Convert into Lowercase = ") upch, _ := reader.ReadByte() if upch >= 'A' && upch <= 'Z' { lw := upch + 32 fmt.Printf("The Lowercase Character of %c = %c\n", upch, lw) } else { fmt.Printf("Either You entered the Lowercase Char or Inalid Alphabet\n") } }
SureshMac:GoExamples suresh$ go run charToLower4.go
Enter Character to Convert into Lowercase = N
The Lowercase Character of N = n
SureshMac:GoExamples suresh$ go run charToLower4.go
Enter Character to Convert into Lowercase = j
Either You entered the Lowercase Char or Inalid Alphabet