[Golang]力扣Leetcode - 709. 转换成小写字母

题目:给你一个字符串 s ,将该字符串中的大写字母转换成相同的小写字母,返回新的字符串。

链接: .

示例1:

输入:s = “Hello” 输出:“hello”

示例 2:

输入:s = “here” 输出:“here”

示例 3:

输入:s = “LOVELY” 输出:“lovely”

思路: 用 range 遍历字符串,如遇到大写字母,其 ASCII 码就加 32 使其变为小写字母,再用 append 把字母一个个添加进 res 输出最后的字符串。

    大写字母 A - Z 的 ASCII 码范围为 [65, 90]; 小写字母 a - z 的 ASCII 码范围为 [97, 122]。

Go代码:

package main

import "fmt"

func toLowerCase(s string) string {
          
   
	var res []int32
	for _, ch := range s {
          
   
		if ch >= 65 && ch <= 90 {
          
   
			ch += 32
		}
		res = append(res, ch)
	}
	return string(res)
}

func main() {
          
   
	fmt.Println(toLowerCase("Hello"))
}
经验分享 程序员 微信小程序 职场和发展