题解 | #字符串加解密#
字符串加解密
https://www.nowcoder.com/practice/2aa32b378a024755a3f251e75cbf233a
package main import ( "fmt" ) func encode(s string) string { size := len(s) sb := []byte(s) for i:=0; i<size; i++ { ch := s[i] if 'a' <= s[i] && s[i] <= 'z' { if s[i] == 'z' { ch = 'A' } else { ch = s[i] + 1 - 32 } } if 'A' <= s[i] && s[i] <= 'Z' { if s[i] == 'Z' { ch = 'a' } else { ch = s[i] + 1 + 32 } } if '0' <= s[i] && s[i] <= '9' { if s[i] == '9' { ch = '0' } else { ch = s[i] + 1 } } sb[i] = ch } return string(sb) } func decode(s string) string { size := len(s) sb := []byte(s) for i:=0; i<size; i++ { ch := s[i] if 'a' <= s[i] && s[i] <= 'z' { if s[i] == 'a' { ch = 'Z' } else { ch = s[i] - 32 - 1 } } if 'A' <= s[i] && s[i] <= 'Z' { if s[i] == 'A' { ch = 'z' } else { ch = s[i] -1 + 32 } } if '0' <= s[i] && s[i] <= '9' { if s[i] == '0' { ch = '9' } else { ch = s[i] - 1 } } sb[i] = ch } return string(sb) } func main() { var encodePassword string var decodePassword string fmt.Scan(&encodePassword, &decodePassword) fmt.Println(encode(encodePassword)) fmt.Println(decode(decodePassword)) }
// 保证输入的字符串都是只由大小写字母或者数字组成 // 本题输入为一行两个字符串,中间空空格分开,所以采用:fmt.Scan(&encodePassword, &decodePassword)