Created
March 9, 2019 21:40
-
-
Save danx12/9145f31db4c84c9da6620fd3415e0e14 to your computer and use it in GitHub Desktop.
Exercise: rot13Reader - A Tour of Go
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 (rot rot13Reader) Read(b []byte) (int, error) { | |
n, err := rot.r.Read(b) | |
for i := range b { | |
if b[i] >= 0x41 && b[i] <= 0x5a { | |
b[i] = byte((int(b[i]) - 0x41 + 13 )% 26 + 0x41) | |
} else if b[i] >= 0x61 && b[i] <= 0x7a { | |
b[i] = byte((int(b[i]) - 0x61 + 13 )% 26 + 0x61) | |
} | |
} | |
return n, err | |
} | |
func main() { | |
//Lbh penpxrq gur pbqr! | |
s := strings.NewReader("You cracked the code!") | |
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