.。oO(さっちゃんですよヾ(〃l _ l)ノ゙☆)

.。oO(此のblogは、主に音樂考察Programmingに分類されますよ。ヾ(〃l _ l)ノ゙♬♪♡





#golang §59 Exercise: Rot13 Reader

A Tour of Go chapter 59

Exercise: Rot13 Reader

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 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.


package main

import (

type rot13Reader struct {
	r io.Reader

func (reader *rot13Reader) Read(p []byte) (n int, err error) {
	n, err = reader.r.Read(p)
	for i := 0; i < len(p); i++ {
		if ('A' <= p[i] && p[i] < 'Z' - 12) || ('a' <= p[i] && p[i] < 'z' - 12) {
			p[i] += 13
		} else {
			p[i] -= 13

func main() {
	s := strings.NewReader(
		"Lbh penpxrq gur pbqr!")
	r := rot13Reader{s}
	io.Copy(os.Stdout, &r)