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)
}

posted @ 2019-11-02 22:57  WangZhuo2000  阅读(294)  评论(0编辑  收藏  举报