I'm having some trouble with tour.golang.org/methods/23
package main
import (
"io"
"os"
"strings"
)
type rot13Reader struct {
r io.Reader
}
func (r rot13Reader) Read(s []byte) (int, error) {
a, e := r.r.Read(s)
for i := 0; i < a; i++ {
if(s[i] >= 'a') {
s[i] = ((s[i] - 'a') + 13) % 26 + 'a'
}
if(s[i] >= 'A') {
s[i] = ((s[i] - 'A') + 13) % 26 + 'A'
}
}
return a, e
}
func main() {
s := strings.NewReader("Lbh penpxrq gur pbqr!")
r := rot13Reader{s}
io.Copy(os.Stdout, &r)
}
I'm adding then modding but the results don't quite look right after accounting for the gap between the ascii codes for lower and upper case
YHN VKTVDXW MAX VHWX!
'A'
is (numerically speaking) 65. Letters deeper in the alphabet increase, up to 'Z'
which is 90.
'a'
is (numerically speaking) 97. Letters deeper in the alphabet increase, up to 'z'
which is 122.
(Don't ask why I have some ASCII memorized. I did have to look up the z
values. :-) )
Exercise 1: if s[i]
is 'a'
, i.e., 97, and you rot-13 it to 'n'
(110), is this greater than 65? What happens when you get to your second if
statement?
Exercise 2: if s[i]
is '_'
(decimal 95), what happens here?
(You keep changing your question, so by now these exercises are somewhat moot.)
If you're working with the ascii values of the letters directly, you have to offset them. The ascii values of capital A-Z are 65-90 inclusive, and lowercase a-z are 97-122 inclusive.
Make sure to only do letters by fixing the bounds on letter checking
between 'a' and 'z' and between 'A' and 'Z'
your second Z was lowercase when it needs to be uppercase