Skip to content

Commit 5ac0bd5

Browse files
soypatsiriak
andauthored
Correct types in caesar.go (TheAlgorithms#331)
Co-authored-by: Andrii Siriak <[email protected]>
1 parent e498a69 commit 5ac0bd5

File tree

1 file changed

+7
-6
lines changed

1 file changed

+7
-6
lines changed

ciphers/caesar/caesar.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,16 @@ package caesar
66
func Encrypt(input string, key int) string {
77
// if key is negative value,
88
// updates "key" the number which congruents to "key" modulo 26
9-
key = (key%26 + 26) % 26
9+
key8 := byte(key%26+26) % 26
1010

11-
outputBuffer := []byte{}
11+
var outputBuffer []byte
12+
// r is a rune, which is the equivalent of uint32.
1213
for _, r := range input {
1314
newByte := byte(r)
14-
if 'A' <= newByte && newByte <= 'Z' {
15-
outputBuffer = append(outputBuffer, byte(int('A')+int(int(newByte-'A')+key)%26))
16-
} else if 'a' <= newByte && newByte <= 'z' {
17-
outputBuffer = append(outputBuffer, byte(int('a')+int(int(newByte-'a')+key)%26))
15+
if 'A' <= r && r <= 'Z' {
16+
outputBuffer = append(outputBuffer, 'A'+(newByte-'A'+key8)%26)
17+
} else if 'a' <= r && r <= 'z' {
18+
outputBuffer = append(outputBuffer, 'a'+(newByte-'a'+key8)%26)
1819
} else {
1920
outputBuffer = append(outputBuffer, newByte)
2021
}

0 commit comments

Comments
 (0)