Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rot13 fix and extending functionality #38

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 16 additions & 12 deletions 015_understanding-TCP-servers/07_tcp-apps/01_rot13/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"fmt"
"log"
"net"
"strings"
"unicode"
)

func main() {
Expand All @@ -28,23 +28,27 @@ func main() {
func handle(conn net.Conn) {
scanner := bufio.NewScanner(conn)
for scanner.Scan() {
ln := strings.ToLower(scanner.Text())
bs := []byte(ln)
r := rot13(bs)
ln := scanner.Text()
r := rot13(ln)

fmt.Fprintf(conn, "%s - %s\n\n", ln, r)
}
}

func rot13(bs []byte) []byte {
var r13 = make([]byte, len(bs))
for i, v := range bs {
// ascii 97 - 122
if v <= 109 {
r13[i] = v + 13
func rot13(ln string) string {
var r13 = make([]rune, len(ln))
for i, v := range ln {
if unicode.IsLower(v) && v < unicode.MaxASCII {
r13[i] = rotateCharacter(v, 'a')
} else if unicode.IsUpper(v) && v < unicode.MaxASCII {
r13[i] = rotateCharacter(v, 'A')
} else {
r13[i] = v - 13
r13[i] = v
}
}
return r13
return string(r13)
}

func rotateCharacter(v rune, base rune) rune {
return (v - base + 13) % 26 + base
}