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)
}
上一篇:day07 前七天学习内容总复习


下一篇:Python(四)装饰器、迭代器&生成器、re正则表达式、字符串格式化