Caesar Cipher

Sort by

recency

|

598 Discussions

|

  • + 0 comments

    Python solution:

    def caesarCipher(string: str, k: int) -> str:
        if k == 0:
            return string
        vocab = "abcdefghijklmnopqrstuvwxyz"
        cipher = []
        for char in string:
            if not char.isalpha():
                cipher.append(char)
            else:
                idx = vocab.index(char.lower())   # index of character
                new_char = vocab[(idx + k) % len(vocab)]    # shift index
                # capitalise if required:
                new_char = new_char.upper() if char.isupper() else new_char 
                cipher.append(new_char)
        return "".join(cipher)
    
  • + 0 comments
     public static String caesarCipher(String s, int k) {
        // Write your code here
            char[] encrypString=new char[s.length()];
            if(k>26){
                int it = k/26;
                k = k - (it*26);
            }
            for(int i =0;i<s.length();i++){
                int ascii = s.charAt(i);
                System.out.println(ascii);
                if(ascii >='a' && ascii <= 'z'){
                    ascii = s.charAt(i) + k;
                    if(ascii > 'z'){
                        ascii = ascii - (26);
                    }
                }
                if(ascii >='A' && ascii <= 'Z'){
                    ascii = s.charAt(i) + k;
                    if(ascii > 'Z'){
                        ascii = ascii - (26);
                    }
                }
                encrypString[i] = (char) ascii;
            }
            return new String(encrypString);
        }
    
    }
    
  • + 0 comments
    #!/bin/python3
    
    import math
    import os
    import random
    import re
    import sys
    
    #
    # Complete the 'caesarCipher' function below.
    #
    # The function is expected to return a STRING.
    # The function accepts following parameters:
    #  1. STRING s
    #  2. INTEGER k
    #
    
    def caesarCipher(s, k):
        # Write your code here
        # time is O(n)
        # incase k is larger than 26, we % so move is between 0 - 26
        move = k % 26
        result = []
        for letter in s:
            if letter.isupper():
                # ord() converts str to ACII num, and chr converts it back
                # - 65 so it goes from 0 - 26, % 26 so after adding move it's 
                # still within 26 letter range, + 65 to go back to ACII
                letter = chr((ord(letter) - 65 + move) % 26 + 65)
            elif letter.islower():
                # lower letter a starts at 97
                letter = chr((ord(letter) - 97 + move)% 26 + 97)
            result.append(letter)
            #combind letters in the arr result with "" (nothing) in between
        return "".join(result)
        
    
    if __name__ == '__main__':
        fptr = open(os.environ['OUTPUT_PATH'], 'w')
    
        n = int(input().strip())
    
        s = input()
    
        k = int(input().strip())
    
        result = caesarCipher(s, k)
    
        fptr.write(result + '\n')
    
        fptr.close()
    
  • + 0 comments

    Time Complexity : O(n) Space Complexity: O(1)

    Java 8

    // a-z 97-122 // A-Z 65- 90

        if(k==0){ return s;}
    
        k=k%26;
    
        int A = 65;
        int Z = 90;
        int a = 97;
        int z = 122;
    
        for(int i=0; i<s.length(); i++){
    
            int currentChar = s.charAt(i);
            int newChar = currentChar + k;
            char intToCharConversion;
            if(currentChar >= A && currentChar <= Z){
                if(newChar > Z){
                    int updatedChar = (A - 1)+ (newChar - Z);
                    intToCharConversion = (char)updatedChar;
                    s =  s.substring(0, i) + intToCharConversion + s.substring(i + 1);
                }else{
                    intToCharConversion = (char)newChar;
                    s =  s.substring(0, i) + intToCharConversion + s.substring(i + 1);
                }
            }else if(currentChar >= a && currentChar <= z){
                if(newChar > z){
                    int updatedChar = (a - 1)+ (newChar - z);
                    intToCharConversion = (char)updatedChar;
                    s =  s.substring(0, i) + intToCharConversion + s.substring(i + 1);
                }else{
                    intToCharConversion = (char)newChar;
                    s =  s.substring(0, i) + intToCharConversion + s.substring(i + 1);
                }
            }
        }    
    
        return s;
    
  • + 0 comments

    In the Go example their code fail to compile...