From 5ac0bd5719c60713ff73a5b1ef8c40b075a78d1a Mon Sep 17 00:00:00 2001 From: Patricio Whittingslow Date: Tue, 7 Sep 2021 02:38:00 -0300 Subject: [PATCH] Correct types in caesar.go (#331) Co-authored-by: Andrii Siriak --- ciphers/caesar/caesar.go | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/ciphers/caesar/caesar.go b/ciphers/caesar/caesar.go index 902f9c114..21da4f5a9 100644 --- a/ciphers/caesar/caesar.go +++ b/ciphers/caesar/caesar.go @@ -6,15 +6,16 @@ package caesar func Encrypt(input string, key int) string { // if key is negative value, // updates "key" the number which congruents to "key" modulo 26 - key = (key%26 + 26) % 26 + key8 := byte(key%26+26) % 26 - outputBuffer := []byte{} + var outputBuffer []byte + // r is a rune, which is the equivalent of uint32. for _, r := range input { newByte := byte(r) - if 'A' <= newByte && newByte <= 'Z' { - outputBuffer = append(outputBuffer, byte(int('A')+int(int(newByte-'A')+key)%26)) - } else if 'a' <= newByte && newByte <= 'z' { - outputBuffer = append(outputBuffer, byte(int('a')+int(int(newByte-'a')+key)%26)) + if 'A' <= r && r <= 'Z' { + outputBuffer = append(outputBuffer, 'A'+(newByte-'A'+key8)%26) + } else if 'a' <= r && r <= 'z' { + outputBuffer = append(outputBuffer, 'a'+(newByte-'a'+key8)%26) } else { outputBuffer = append(outputBuffer, newByte) }