zoukankan      html  css  js  c++  java
  • Exercise: Rot13 Reader

    package main
    
    import (
        "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.

  • 相关阅读:
    筛选法 || POJ 1356 Prime Land
    搜索 || BFS || POJ 3278 Catch That Cow
    (素数筛) 找质数
    (map)后缀字符串
    字符串的进制
    (二进制枚举子集)买玩具
    (基础)01背包问题
    (基础)编辑距离
    (基础)最长公共字串
    最大子矩阵和
  • 原文地址:https://www.cnblogs.com/ghgyj/p/4058280.html
Copyright © 2011-2022 走看看