Skip to content

Instantly share code, notes, and snippets.

@c-yan
Created January 18, 2018 21:58
Show Gist options
  • Save c-yan/78b9d35a14f2212b0459f2a928241c9b to your computer and use it in GitHub Desktop.
Save c-yan/78b9d35a14f2212b0459f2a928241c9b to your computer and use it in GitHub Desktop.
[A Tour of Go] Exercise: rot13Reader
package main
import (
"io"
"os"
"strings"
)
type rot13Reader struct {
r io.Reader
}
func rot13(b byte) byte {
if b <= 'A' || b > 'z' {
return b
}
if b >= 'a' {
return (((b - 'a') + 13) % 26) + 'a'
} else {
return (((b - 'A') + 13) % 26) + 'A'
}
}
func (r rot13Reader) Read(p []byte) (n int, err error) {
n, err = r.r.Read(p)
for i := range p {
p[i] = rot13(p[i])
}
return
}
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