Created
July 18, 2012 02:47
-
-
Save jadeallenx/3133785 to your computer and use it in GitHub Desktop.
learngo rot13 solution
This file contains hidden or 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 (r13 *rot13Reader) Read(p []byte) (n int, err error) { | |
n, err = r13.r.Read(p) | |
const A = byte(65) | |
const z = byte(122) | |
const base = byte(96) | |
for i := 0; i < n; i++ { | |
if p[i] >= A && p[i] <= z { | |
p[i] += 13 | |
if p[i] > z { | |
p[i] = base + ( p[i] - z ) | |
} | |
} | |
} | |
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