Created
April 4, 2015 17:08
-
-
Save evandertino/037cd27095d12d06a222 to your computer and use it in GitHub Desktop.
GO Exercise: rot13Reader - http://tour.golang.org/methods/12
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import ( | |
| "io" | |
| "os" | |
| "strings" | |
| ) | |
| type rot13Reader struct { | |
| r io.Reader | |
| } | |
| func (this *rot13Reader) Read(bytes []byte) (num int, err error) { | |
| num, err = this.r.Read(bytes) | |
| for index := range bytes { | |
| if (bytes[index] >= 'A' && bytes[index] <= 'M') || (bytes[index] >='a' && bytes[index] <= 'm') { | |
| bytes[index] += 13 | |
| } else if (bytes[index] >= 'N' && bytes[index] <= 'Z') || (bytes[index] >= 'n' && bytes[index] <= 'z'){ | |
| bytes[index] -= 13 | |
| } | |
| } | |
| return | |
| } | |
| func main() { | |
| s := strings.NewReader("Lbh penpxrq gur pbqr!") | |
| r := rot13Reader{s} | |
| io.Copy(os.Stdout, &r) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment