Caesar Cipher

Sort by

recency

|

612 Discussions

|

  • + 0 comments

    Java with explain.

    String alphabet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; //26+26 symbols
            String result = "";
            k = k % 26;//correct rotate amount
            for (int i = 0; i < s.length(); i++) {
                Character currentChar = s.charAt(i);
                int index = alphabet.indexOf(currentChar); //get index of char
                if (index < 26 && index + k > 25 //correct index if it goes abroad
                        || index < 52 && index + k > 51) { //for uppercase
                    index -= 26;
                }
                if (Character.isLetter(currentChar)) { //if it not a char (-' e.t.c)
                    result = result + alphabet.charAt(index + k);
                } else {
                    result = result + s.charAt(i);
                }
            }
            return result;
    
  • + 0 comments
    def caesarCipher(s, k):
        # Write your code here
        return "".join([chr(((ord(char) + (k % 26) - 97) % 26) + 97) if char.islower() else chr(((ord(char) + (k % 26) - 65) % 26) + 65) if char.isalpha() else char for char in s])
    
  • + 0 comments

    Scala solution

    def caesarCipher(s: String, k: Int): String = {
        // Write your code here
            val shift = k % 26
            s.map {char => if (char.isLower){((char - 'a' + shift) % 26 + 'a').toChar}
            else if (char.isUpper){((char - 'A' + shift) % 26 + 'A').toChar}
            else {char}}.mkString
    
        }
    
  • + 0 comments
    lowercase_alphabet = string.ascii_lowercase
        uppercase_alphabet = string.ascii_uppercase
    
        result = []
        for letter in s:
            if letter in lowercase_alphabet:
                x = lowercase_alphabet.index(letter)
                new_x = (x + k) % len(lowercase_alphabet)
                result.append(lowercase_alphabet[new_x])
            elif letter in uppercase_alphabet:
                x = uppercase_alphabet.index(letter)
                new_x = (x + k) % len(uppercase_alphabet)
                result.append(uppercase_alphabet[new_x])
            else:
                result.append(letter)
    
        return ''.join(result)
    
  • + 0 comments

    Python

    from itertools import cycle, islice
    
    def caesarCipher(s, k):
        if k == 0: return s
            
        # shift alphabet
        alpha = string.ascii_lowercase
        shifted = list(islice(cycle(alpha),
                              k, k+len(alpha)
                              ))
        
        s_shift = []
        for char in s:
            # uppercase flag
            upper = False
            if char.isupper():
                upper = True
                char = char.lower()
                
            # handling non-letters
            match = re.match(r'\W', char)
            if match or char.isnumeric() or char == '_':
                s_shift.append(char)
                
            # cipher
            else:
                idx = alpha.index(char)
                new_char = str(shifted[idx])
                if upper: new_char = new_char.upper()
                s_shift.append(new_char)
    						
        return str(''.join(s_shift))