~telemachus/Exercism

ref: 392885548959505a17ef8731a06b37a972bdd1df Exercism/go/atbash-cipher/atbash_cipher.go -rw-r--r-- 521 bytes
39288554Peter Aronoff Atbash cipher 6 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
package atbash

import (
	"strings"
)

func Atbash(s string) string {
	var atbash strings.Builder
	for _, b := range []byte(s) {
		// This leaves an unwanted space at the end of the string. Trim that off
		// later.
		if atbash.Len()%6 == 5 {
			atbash.WriteByte(' ')
		}
		switch {
		case b >= 'a' && b <= 'z':
			atbash.WriteByte('z' - b + 'a')
		case b >= 'A' && b <= 'Z':
			atbash.WriteByte('z' - b + 'A')
		case b >= '0' && b <= '9':
			atbash.WriteByte(b)
		}
	}
	return strings.TrimSuffix(atbash.String(), " ")
}