Skip to content

Caeser cipher #517

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/algorithms/string/caeserCipher/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# caeserCipher Algorithm

caeserCipher Algorithm is a type of substitution algorithm in which each letter in the plaintext is replaced by a letter some fixed number of positions down the alphabet. For example, with a left shift of 3, D would be replaced by A, E would become B, and so on

## Complexity

- **Time:** `O(|n|)`
- **Space:** `O(|n|)`
## Example

The the following string `abcde` which is shifted by 1 will change to `bcdef`

## References

- [Wikipedia](https://en.wikipedia.org/wiki/Caesar_cipher)
27 changes: 27 additions & 0 deletions src/algorithms/string/caeserCipher/__test__/caeserCipher.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import caeserCipher from '../caeserCipher';

describe('caeserCipher', () => {
it('should subtitute each character by shifting up the alphabet by a given integer', () => {


expect(caeserCipher('abcd', 1)).toBe('bcde');
});


it('should wrap back to the beginning of the alphabet if it shifts more than the 26 english alphabets', () => {


expect(caeserCipher('xyz', 1)).toBe('yza');
});
it('should only shift letters and ignore non-alphabetic characters', () => {

expect(caeserCipher('gurer ner 9 qbtf!', 13)).toBe('there are 9 dogs!');
});

it('should ignore case sensitivity', () => {

expect(caeserCipher('ABCD', 13)).toBe('bcde');
});


})
30 changes: 30 additions & 0 deletions src/algorithms/string/caeserCipher/caeserCipher.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* @param {string} string
* @param {number} shift
* @return {string} result
*/

export default function caeserCipher(string, shift) {
// convert all alphabets in english language to an array
const alphabetArr = "abcdefghijklmnopqrstuvwxyz".split("")
// converting all the alphabets in string to lowercase
string = string.toLowerCase()

let result = ""
for (let i = 0; i < string.length; i++) {
const currentChar = string[i]
// checking index of character in the english alphabets
const idx = alphabetArr.indexOf(currentChar)
// skip character if the character is not an alphabet
if (idx === -1) {
result += currentChar
continue;
}
//wrap up index, incase the next shift is beyond the 26th character
const encodedIdx = (idx + shift) % 26
result += alphabetArr[encodedIdx]

}
// return the result of the shifted string
return result
}