Caesar Cipher

Sort by

recency

|

243 Discussions

|

  • + 1 comment
    def caesarCipher(s, k):
        encripted=''
        for i in s:
            if i.isalpha():
                if i.islower():
                    encripted+=chr((ord(i)-ord('a')+k)%26+ord('a'))
                elif i.isupper():
                    encripted+=chr((ord(i)-ord('A')+k)%26+ord('A'))
            else:
                encripted+=i
        return encripted
    
  • + 0 comments

    Solution in javascript:

    function caesarCipher(s, k) {
        // Write your code here
        const alphabet = Array.from('abcdefghijklmnopqrstuvwxyz');
        const upAlphabet = Array.from('abcdefghijklmnopqrstuvwxyz'.toUpperCase());
        let l = []
        const arr = Array.from(s)
        const f = k%26
        arr.forEach(char => {
            const isLetter = alphabet.includes(char) || upAlphabet.includes(char)
            const al = alphabet.includes(char) && isLetter ? alphabet : upAlphabet; 
            if (isLetter) {
                const index = al.indexOf(char);
                if (index + k > al.length) {
                    const dex = index + f >= al.length ? (index + f) - al.length : index + f
                    l.push(al[dex])
                } else if (index + k == al.length) {
                    l.push(al[0])
                } else {
                    l.push(al[index + f])
                } 
            } else {
                l.push(char)
            }
        })
        return l.join('');
    }
    
  • + 0 comments
    def caesarCipher(s, k):
        # Write your code here
        encrypted = []
        for char in s:
            if char.isalpha():
                if char.islower():
                    new_char = chr(((ord(char)-ord('a')+k)%26)+ord('a'))
                elif char.isupper():
                    new_char = chr(((ord(char)-ord('A')+k)%26)+ord('A'))
                encrypted.append(new_char)
            else:
                encrypted.append(char)
        return ''.join(encrypted)
    
  • + 1 comment

    Python3

    alphabet = "abcdefghijklmnopqrstuvwxyz"
    alphabet_cap = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    encrypted = ""
    
    #calculate new k if k is greater than 26
    if k > 26:
        k = k % 26
    
    #rotate alphabet by k
    rotated_alphabet = alphabet[k:] + alphabet[:k]
    rotated_alphabet_cap = alphabet_cap[k:] + alphabet_cap[:k]
    
    
    #encrypt
    for i in range(len(s)):
    
        #uppercase letters
        if s[i].isupper():
            index = alphabet_cap.index(s[i])
            encrypted += rotated_alphabet_cap[index]
    
        #lowercase letters
        elif s[i].islower():
            index = alphabet.index(s[i])
            encrypted += rotated_alphabet[index]
    
        #punctuation, hyphens, etc
        else:
            encrypted += s[i]
    
    return encrypted
    
  • + 0 comments

    Kotlin Solution

    fun caesarCipher(s: String, k: Int): String {
        return buildString{
            s.map{ letter ->
                when {
                    letter in 'a'..'z' -> append('a' + (letter - 'a' + k) % 26) 
                    letter in 'A'..'Z' -> append('A' + (letter - 'A' + k) % 26) 
                    else -> append(letter)
                }
            }
        }
    }