Last active
July 9, 2016 17:32
-
-
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)
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
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