Skip to content

Instantly share code, notes, and snippets.

@nilsmagnus
Created January 24, 2015 20:57
Show Gist options
  • Save nilsmagnus/e13b12b832dba3179d86 to your computer and use it in GitHub Desktop.
Save nilsmagnus/e13b12b832dba3179d86 to your computer and use it in GitHub Desktop.
rot 13 reader from exercise of the tour
package main
import (
"io"
"os"
"strings"
)
type rot13Reader struct {
r io.Reader
}
func (rot *rot13Reader) Read(b []byte) (int, error){
n, err := rot.r.Read(b)
for i:=0; i< n ; i++ {
if (b[i] >= 97 && b[i] <= 122) {
b[i] = b[i] + 13
if b[i] > 122 {
b[i] = b[i] % 122 + 96
}
} else if (b[i] >= 65 && b[i] <= 90) {
b[i] = b[i] + 13
if b[i] > 90 {
b[i] = b[i] % 90 + 64
}
}
}
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