题解 | #字符串变形#
字符串变形
https://www.nowcoder.com/practice/c3120c1c1bc44ad986259c0cf0f0b80e
package main import "strings" /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param s string字符串 * @param n int整型 * @return string字符串 */ func trans( s string , n int ) string { // 分割单词 words := strings.Split(s, " ") // 反转单词 for i,j := 0, len(words) -1;i < j; i, j = i+1,j-1 { words[i],words[j] = words[j],words[i] } // 反转单词大小写 for i, word := range words { runes := []rune(word) for j,ch := range runes { if ch >= 'a' && ch <= 'z' { runes[j] = ch - 'a' + 'A' }else if ch >= 'A' && ch <= 'Z' { runes[j] = ch - 'A' + 'a' } } words[i] = string(runes) } return strings.Join(words," ") }