File tree 1 file changed +7
-6
lines changed 1 file changed +7
-6
lines changed Original file line number Diff line number Diff line change @@ -6,15 +6,16 @@ package caesar
6
6
func Encrypt (input string , key int ) string {
7
7
// if key is negative value,
8
8
// updates "key" the number which congruents to "key" modulo 26
9
- key = (key % 26 + 26 ) % 26
9
+ key8 := byte (key % 26 + 26 ) % 26
10
10
11
- outputBuffer := []byte {}
11
+ var outputBuffer []byte
12
+ // r is a rune, which is the equivalent of uint32.
12
13
for _ , r := range input {
13
14
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 )
18
19
} else {
19
20
outputBuffer = append (outputBuffer , newByte )
20
21
}
You can’t perform that action at this time.
0 commit comments