Created
October 10, 2016 13:08
-
-
Save danimal141/cb5c621f1a496be4fd48f6848341d043 to your computer and use it in GitHub Desktop.
A Tour of Go Exercise: rot13Reader
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) | |
if err != nil { | |
return n, err | |
} | |
for i := range b { | |
switch { | |
case (b[i] >= 'A' && b[i] < 'N'), (b[i] >='a' && b[i] < 'n'): b[i] += 13 | |
case (b[i] > 'M' && b[i] <= 'Z'), (b[i] > 'm' && b[i] <= 'z'): b[i] -= 13 | |
} | |
} | |
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