其他分享
首页 > 其他分享> > Go语言练习 Rot13

Go语言练习 Rot13

作者:互联网

Go语言练习 Rot13

地址:https://tour.go-zh.org/methods/23

package main

import (
    "io"
    "os"
    "strings"
)

type rot13Reader struct {
    r io.Reader
}

func rot13(x byte) byte{
    lower := x<='z'&&x>='a'
    upper := x<='Z'&&x>='A'
    if (!lower)&&(!upper){
        return x
    }
    x += 13
    if lower&&x>'z'{
        return x-26
    }
    if upper&&x>'Z'{
        return x-26
    }
    return x
}

func (rot *rot13Reader) Read(b []byte) (n int,e error){
    n,e = rot.r.Read(b)
    for i:=0;i<n;i++{
        b[i] = rot13(b[i])
    }
    return n,e
}

func main() {
    s := strings.NewReader("Lbh penpxrq gur pbqr!")
    r := rot13Reader{s}
    io.Copy(os.Stdout, &r)
}

标签:upper,lower,return,练习,Rot13,&&,Go,byte
来源: https://www.cnblogs.com/zhuowangy2k/p/11784654.html