Created
January 24, 2015 20:57
-
-
Save nilsmagnus/e13b12b832dba3179d86 to your computer and use it in GitHub Desktop.
rot 13 reader from exercise of the tour
This file contains 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(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