Created
June 11, 2020 01:28
-
-
Save zh-f/8ad522f070ec05eec0bd60720ec95ad9 to your computer and use it in GitHub Desktop.
Solution of 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 | |
} | |
//ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz | |
const src string = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" | |
//NOPQRSTUVWXYZABCDEFGHIJKLMnopqrstuvwxyzabcdefghijklm | |
const dest string = "NOPQRSTUVWXYZABCDEFGHIJKLMnopqrstuvwxyzabcdefghijklm" | |
func (r *rot13Reader) Read (b []byte) (n int, err error) { | |
m, err := r.r.Read(b) | |
for i := 0; i < m; i++ { | |
if b[i] >= 'A' && b[i] <= 'Z' || b[i] >= 'a' && b[i] <= 'z' { | |
b[i] = dest[strings.IndexByte(src, b[i])] | |
} | |
} | |
return m, 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