Skip to content

Instantly share code, notes, and snippets.

@Aadithya-V
Last active November 24, 2022 10:11
Show Gist options
  • Save Aadithya-V/87c0013e6fb1c8786f7b7995b5e4184f to your computer and use it in GitHub Desktop.
Save Aadithya-V/87c0013e6fb1c8786f7b7995b5e4184f to your computer and use it in GitHub Desktop.
Go Tour rot13 Solution
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