Solved rot13 exercise

This commit is contained in:
Stephen M. McQuay
2012-09-03 07:52:35 -06:00
parent 26d12054a1
commit a3f6157543
+13 -5
View File
@@ -11,15 +11,23 @@ type rot13Reader struct {
} }
func (r rot13Reader) Read(p []byte) (n int, err error) { func (r rot13Reader) Read(p []byte) (n int, err error) {
if len(p) == 0 { n, err = r.r.Read(p)
return 0, nil for i := range p[:n] {
} p[i] = rot13(p[i])
if r.i >= len(r.s) {
return 0, io.EOF
} }
return return
} }
func rot13(b byte) byte {
var a, z, A, Z byte = 'a', 'z', 'A', 'Z'
if a < b && b < z {
b = a + ((b - a + 13) % 26)
} else if A < b && b < Z {
b = A + ((b - A + 13) % 26)
}
return b
}
func main() { func main() {
s := strings.NewReader("Lbh penpxrq gur pbqr!") s := strings.NewReader("Lbh penpxrq gur pbqr!")
r := rot13Reader{s} r := rot13Reader{s}