Created
June 3, 2020 13:50
-
-
Save nexus166/c27b0f3df9bd541bfd8fbe6e221cb013 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func rotateString(s string, n int, encdec bool) (string, error) { | |
l := len(s) | |
if l < 1 { | |
return "", fmt.Errorf("empty input") | |
} | |
if n < 1 || n > 6 { | |
return "", fmt.Errorf("invalid shift n %d", n) | |
} | |
var out string | |
switch encdec { | |
case true: // obfuscate | |
for i := 0; i < l; i++ { | |
out += string(int(s[i]) + n) | |
} | |
case false: // deobfuscate | |
for i := 0; i < l; i++ { | |
out += string(int(s[i]) - n) | |
} | |
} | |
return out, nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment