Created
April 5, 2021 17:03
-
-
Save Nishith-Savla/7e370f1fccd8b7b4d169cd7e4a9d86e8 to your computer and use it in GitHub Desktop.
A Tour of Go Exercise: rot13reader (Methods and interfaces module 23)
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 (rot *rot13Reader) Read(b []byte) (int, error) { | |
n, err := rot.r.Read(b) | |
if err == nil { | |
firstLetter := byte(0) | |
for i := 0; i < len(b); i++ { | |
if b[i] >= 'A' && b[i] <= 'Z' { | |
firstLetter = 'A' | |
} else if b[i] >= 'a' && b[i] <= 'z' { | |
firstLetter = 'a' | |
} else { | |
continue | |
} | |
b[i] = firstLetter + ((b[i] - firstLetter + 13) % 26) | |
} | |
} | |
return n, err | |
} | |
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