Skip to content

Instantly share code, notes, and snippets.

@ldco2016
Forked from primaryobjects/caesar-cipher.js
Created November 12, 2017 18:26
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ldco2016/918647ab90c5b6478d83c06e51af6833 to your computer and use it in GitHub Desktop.
Save ldco2016/918647ab90c5b6478d83c06e51af6833 to your computer and use it in GitHub Desktop.
Caesar Cipher algorithm, encrypt string wrap back around to letters
process.stdin.resume();
process.stdin.setEncoding('ascii');
var input_stdin = "";
var input_stdin_array = "";
var input_currentline = 0;
process.stdin.on('data', function (data) {
input_stdin += data;
});
process.stdin.on('end', function () {
input_stdin_array = input_stdin.split("\n");
main();
});
function readLine() {
return input_stdin_array[input_currentline++];
}
/////////////// ignore above this line ////////////////////
function main() {
var n = parseInt(readLine());
var s = readLine();
var k = parseInt(readLine());
var result = '';
for (var i=0; i<s.length; i++) {
var char = s[i].charCodeAt();
var ch = s[i];
if ((char >= 'A'.charCodeAt() && char <= 'Z'.charCodeAt())) {
char += k;
while (char > 'Z'.charCodeAt()) {
char = 'A'.charCodeAt() + (char - 'Z'.charCodeAt() - 1);
}
ch = String.fromCharCode(char);
}
else if (char >= 'a'.charCodeAt() && char <= 'z'.charCodeAt()) {
char += k;
while (char > 'z'.charCodeAt()) {
char = 'a'.charCodeAt() + (char - 'z'.charCodeAt() - 1);
}
ch = String.fromCharCode(char);
}
result += ch;
}
console.log(result);
}
Julius Caesar protected his confidential information by encrypting it in a cipher. Caesar's cipher rotated every letter in a string by a fixed number, , making it unreadable by his enemies. Given a string, , and a number, , encrypt and print the resulting string.
Note: The cipher only encrypts letters; symbols, such as -, remain unencrypted.
Input Format
The first line contains an integer, , which is the length of the unencrypted string.
The second line contains the unencrypted string, .
The third line contains the integer encryption key, , which is the number of letters to rotate.
Constraints
is a valid ASCII string and doesn't contain any spaces.
Output Format
For each test case, print the encoded string.
Sample Input
11
middle-Outz
2
Sample Output
okffng-Qwvb
Explanation
Each unencrypted letter is replaced with the letter occurring spaces after it when listed alphabetically. Think of the alphabet as being both case-sensitive and circular; if rotates past the end of the alphabet, it loops back to the beginning (i.e.: the letter after is , and the letter after is ).
Selected Examples:
(ASCII 109) becomes (ASCII 111).
(ASCII 105) becomes (ASCII 107).
remains the same, as symbols are not encoded.
(ASCII 79) becomes (ASCII 81).
(ASCII 122) becomes (ASCII 98); because is the last letter of the alphabet, (ASCII 97) is the next letter after it in lower-case rotation.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment