Caesar Cipher

Sort by

recency

|

609 Discussions

|

  • + 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))
    
  • + 0 comments
    def caesarCipher(s, k):
        # Write your code here
    
        k = k - ((k//26) * 26)    # normalising the K
        cipher = ""
        for i in range(len(s)):
            if 64 < ord(s[i]) < 91 :
                val = ord(s[i]) + k if (ord(s[i]) + k) < 91 else 64 + (ord(s[i]) + k) - 90
                cipher = cipher + chr(val)
            elif 96 < ord(s[i]) < 123 :
                val = ord(s[i]) + k if (ord(s[i]) + k) < 123 else 96 + (ord(s[i]) + k) - 122
                cipher = cipher + chr(val)
            else :
                val = ord(s[i])
                cipher = cipher + chr(val)   
        return cipher
    
  • + 0 comments
    def caesarCipher(s, k):
        res=""
        for i in range(len(s)):
            small, ch = check('a','z', s, i, k)
            if not small:
                big, ch = check('A', 'Z', s, i, k)
            res+=ch
        return res
    
    def check(start, end, s, i,  k):
        n = ord(end)-ord(start)+1
        ch = s[i]
        if ord(start) <= ord(ch) <= ord(end):
            i = ord(ch) - ord(start)
            new_i = (i + k) % n
            return True, chr(ord(start) + new_i)
        return False, ch
    
  • + 0 comments

    Simplified Java Solution Method:

    public static String caesarCipher(String s, int k) {
        // Write your code here
            String cipher = "";
            k%=26;
                    
            for(char c : s.toCharArray()){
                if(Character.isLetter(c)){
                    if((Character.isUpperCase(c)&&(c+k)>90)||(Character.isLowerCase(c)&&(c+k)>122)){
                         c-=26;                  
                    }
                    c+=k;
                }
                cipher+=c;   
            }
            return cipher;
        }