exercise2: rot13reader

This commit is contained in:
Aliberk Sandıkçı 2023-07-31 11:11:52 +03:00
parent a3edafb756
commit 805ffe2504
Signed by: asandikci
GPG key ID: 25C67A03B5666BC1

46
exercises/rot13reader.go Normal file
View file

@ -0,0 +1,46 @@
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)
}