Skip to content

Instantly share code, notes, and snippets.

@VojtechVitek
Created April 15, 2014 16:50
Show Gist options
  • Save VojtechVitek/10746955 to your computer and use it in GitHub Desktop.
Save VojtechVitek/10746955 to your computer and use it in GitHub Desktop.
A Tour of Go - Exercise: Rot13 Reader
package main
import (
"io"
"os"
"strings"
)
type rot13Reader struct {
r io.Reader
}
func (r rot13Reader) Read(p []byte) (n int, err error) {
n, err = r.r.Read(p)
for i := 0; i < n; i++ {
if p[i] >= 'a' && p[i] <= 'z' {
p[i] = ((p[i] - 'a' + 13) % ('z' - 'a' + 1)) + 'a'
} else if p[i] >= 'A' && p[i] <= 'Z' {
p[i] = ((p[i] - 'A' + 13) % ('Z' - 'A' + 1)) + 'A'
}
}
return n, err
}
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