exercise2: rot13reader
This commit is contained in:
parent
a3edafb756
commit
805ffe2504
1 changed files with 46 additions and 0 deletions
46
exercises/rot13reader.go
Normal file
46
exercises/rot13reader.go
Normal 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)
|
||||
}
|
Loading…
Reference in a new issue