Exercise: rot13Reader

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 compressed 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 an io.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-rot-reader.go

  1. package main
  2. import (
  3. "io"
  4. "os"
  5. "strings"
  6. )
  7. type rot13Reader struct {
  8. r io.Reader
  9. }
  10. func main() {
  11. s := strings.NewReader("Lbh penpxrq gur pbqr!")
  12. r := rot13Reader{s}
  13. io.Copy(os.Stdout, &r)
  14. }