Created
March 4, 2019 01:55
-
-
Save krry/0be2c3a601781a031a1180c8f08ad719 to your computer and use it in GitHub Desktop.
An answer to A Tour of Go Exercise: Rot13 Reader
This file contains 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
package main | |
import ( | |
"io" | |
"os" | |
"strings" | |
) | |
type rot13Reader struct { | |
r io.Reader | |
} | |
func (rot *rot13Reader) Read(p []byte) (n int, err error) { | |
n, err = rot.r.Read(p) | |
for i := 0; i < len(p); i++ { | |
char := p[i] | |
if (char >= 'A' && char < 'N') || (char >= 'a' && char < 'n') { | |
p[i] += 13 | |
} else if (char > 'M' && char <= 'Z') || (char > 'm' && char <= 'z') { | |
p[i] -= 13 | |
} | |
} | |
return | |
} | |
func main() { | |
s := strings.NewReader("Lhz! N ebg13frevrf puvpxra.") | |
r := rot13Reader{s} | |
io.Copy(os.Stdout, &r) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment