首页 > 代码库 > Exercise: Rot13 Reader
Exercise: Rot13 Reader
package mainimport ( "io" "os" "strings" "fmt")type rot13Reader struct { r io.Reader}func (rot13 rot13Reader)Read(p []byte) (n int, err error){ n,err = rot13.r.Read(p) for i := 0; i < len(p); i++ { if (p[i] >= ‘A‘ && p[i] < ‘N‘) || (p[i] >=‘a‘ && p[i] < ‘n‘) { p[i] += 13 } else if (p[i] > ‘M‘ && p[i] <= ‘Z‘) || (p[i] > ‘m‘ && p[i] <= ‘z‘){ p[i] -= 13 } } return}func main() { s := strings.NewReader( "Lbh penpxrq gur pbqr!") r := rot13Reader{s} fmt.Println(r) io.Copy(os.Stdout, &r)}
go官方教程的答案地址https://gist.github.com/zyxar/2317744
A common pattern is an io.Reader that wraps another io.Reader
, modifying the stream in some way.
For example, the gzip.NewReader function takes an io.Reader
(a stream of gzipped data) and returns a *gzip.Reader
that also implements io.Reader
(a stream of the decompressed data).
Implement a rot13Reader
that implements io.Reader
and reads from anio.Reader
, modifying the stream by applying the ROT13 substitution cipher to all alphabetical characters.
The rot13Reader
type is provided for you. Make it an io.Reader
by implementing its Read
method.
Exercise: Rot13 Reader
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。