go-learning/exercises/rot13reader.go
2023-07-31 11:11:52 +03:00

46 lines
683 B
Go

package main
import (
"io"
"os"
"strings"
)
type rot13Reader struct {
r io.Reader
}
const capitalA = 65
const capitalZ = 89
const capitalMid = 77
const A = 97
const Z = 122
const Mid = 109
func (rot *rot13Reader) Read(b []byte) (n int, err error) {
n, err = rot.r.Read(b)
for i := 0; i < n; i++ {
if b[i] <= capitalZ && b[i] >= capitalA {
if b[i] <= capitalMid {
b[i] = b[i] + 13
} else {
b[i] = b[i] - 13
}
} else if b[i] <= Z && b[i] >= A {
if b[i] <= Mid {
b[i] = b[i] + 13
} else {
b[i] = b[i] - 13
}
}
}
return
}
func main() {
s := strings.NewReader("Lbh penpxrq gur pbqr!")
r := rot13Reader{s}
io.Copy(os.Stdout, &r)
}