dongwuli5105 2019-09-13 14:06
浏览 17
已采纳

rot13密码有什么问题?

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!

展开全部

  • 写回答

3条回答 默认 最新

  • drqja5919276 2019-09-13 14:31
    关注

    '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.)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)
编辑
预览

报告相同问题?