From 805ffe2504d2e07a966840153f077e2b6d49befa Mon Sep 17 00:00:00 2001 From: asandikci Date: Mon, 31 Jul 2023 11:11:52 +0300 Subject: [PATCH] exercise2: rot13reader --- exercises/rot13reader.go | 46 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 exercises/rot13reader.go diff --git a/exercises/rot13reader.go b/exercises/rot13reader.go new file mode 100644 index 0000000..4548a8d --- /dev/null +++ b/exercises/rot13reader.go @@ -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) +}