• + 0 comments

    Here is my solution in python3:

    def caesarCipher(s, k):
        k = k % 26  # Normalize shift value to fall within the range [0, 25]
    
        result = []  # Directly create the result list
    
        # Iterate through the string and apply transformations
        for char in s:
            if char.isalpha():  # Check if the character is alphabetic
                # Calculate shifted character based on case
                base = ord('A') if char.isupper() else ord('a')
                new_char = chr((ord(char) - base + k) % 26 + base)
                result.append(new_char)
            else:
                result.append(char)  # Non-alphabetic characters remain unchanged
    
        return ''.join(result)  # Join list into final string