题解 | #字符串变形#
字符串变形
http://www.nowcoder.com/practice/c3120c1c1bc44ad986259c0cf0f0b80e
//别的大佬写的,我加了注释
func trans(s string, n int) string {
strList := strings.Split(s, " ")
// reverse
for i, j := 0, len(strList)-1; i < j; i, j = i+1, j-1 {
strList[i], strList[j] = strList[j], strList[i]
}
result := make([]string, 0, len(strList))
for i, _ := range strList {
//
temp := []byte(strList[i])
for j, _ := range temp {
//A的ASC值为65,Z为90
//a为97,z为122
//小写变大写
if temp[j] >= 97 {
temp[j] -= 32
//大写变小写
} else if temp[j] <= 90 {
temp[j] += 32
}
}
//将结果转化为string保存进数组
result = append(result, string(temp))
}
//用空格将字符串拼接你起来
return strings.Join(result, " ")
}