Skip to content

Instantly share code, notes, and snippets.

@galch
Last active July 9, 2016 17:32
Show Gist options
  • Save galch/cf0e1fff1534a54269aa336e0031d6dc to your computer and use it in GitHub Desktop.
Save galch/cf0e1fff1534a54269aa336e0031d6dc to your computer and use it in GitHub Desktop.
A Tour of Go - [Exercise : Rot13 Reader](http://tour.golang.org/#61) & [연습: Rot13 Reader](https://go-tour-kr.appspot.com/#61)
package main
import (
"io"
"os"
"strings"
)
type rot13Reader struct {
r io.Reader
}
func (rot rot13Reader) Read(p []byte) (int, error) {
n, err := rot.r.Read(p)
for i, v := range p {
p[i] = rot13(v)
}
return n, err
}
func rot13(c byte) byte {
if (c >= 'a' && c < 'n') || (c >= 'A' && c < 'N') {
c = c + 13
} else if (c >= 'n' && c <= 'z') || (c >= 'N' && c <= 'Z') {
c = c - 13
}
return c
}
func main() {
s := strings.NewReader(
"Lbh penpxrq gur pbqr!")
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