Last active
November 24, 2022 10:11
-
-
Save Aadithya-V/87c0013e6fb1c8786f7b7995b5e4184f to your computer and use it in GitHub Desktop.
Go Tour rot13 Solution
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 rot13(x byte) byte { | |
capital := x >= 'A' && x <= 'Z' | |
if !capital && (x < 'a' || x > 'z') { | |
return x // Not a letter | |
} | |
x += 13 | |
if capital && x > 'Z' || !capital && x > 'z' { | |
x -= 26 | |
} | |
return x | |
} | |
func (r13 *rot13Reader) Read(b []byte) (int, error) { | |
n, err := r13.r.Read(b) | |
for i := 0; i <= n; i++ { | |
b[i] = rot13(b[i]) | |
} | |
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