Skip to content

Commit c53f128

Browse files
committed
Merge pull request algorithm-visualizer#145 from archie94/ceasar-cipher-string-with-space
Ceasar cipher fix-Encrypt string with spaces
2 parents 98d7366 + 5d5527c commit c53f128

File tree

2 files changed

+14
-11
lines changed

2 files changed

+14
-11
lines changed

algorithm/cryptography/caesar_cipher/basic/code.js

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,20 @@ function cipher(str, rotation, direction, cipherTracer) {
2323
cipherTracer._wait();
2424

2525
var currChar = str.charAt(i);
26-
var r = rotation;
27-
28-
logger._print('Rotating ' + currChar + ' ' + direction + ' ' + rotation + ' times');
29-
cipherTracer._select(i)._wait();
30-
31-
// perform given amount of rotations in the given direction
32-
while (r-- > 0) {
33-
currChar = getNextChar(currChar, direction);
34-
cipherTracer._notify(i, currChar)._wait();
26+
if (typeof alphabetMap[currChar] === 'number') { // don't encrpt/decrypt characters not in alphabetMap
27+
var r = rotation;
28+
29+
logger._print('Rotating ' + currChar + ' ' + direction + ' ' + rotation + ' times');
30+
cipherTracer._select(i)._wait();
31+
32+
// perform given amount of rotations in the given direction
33+
while (r-- > 0) {
34+
currChar = getNextChar(currChar, direction);
35+
cipherTracer._notify(i, currChar)._wait();
36+
}
37+
} else {
38+
logger._print('Ignore this character');
3539
}
36-
3740
str = str.substring(0, i) + currChar + str.substring(i + 1);
3841
logger._print('Current result: ' + str);
3942
}

algorithm/cryptography/caesar_cipher/basic/data.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
var string = 'secret';
1+
var string = 'hello! how are you doing?';
22
var rotation = 5;
33
var alphabet = 'abcdefghijklmnopqrstuvwxyz';
44
// create a map of char -> position to improve run time

0 commit comments

Comments
 (0)