Sort by

recency

|

1270 Discussions

|

  • + 0 comments

    Perl:

    sub caesarCipher {
        my @s = split("", shift);
        my $k = shift;
        my %shifted_alphabet;
        my $res;
        for (my $i = 97; $i <= 122; $i++) {
            if ((($k - 1) % 26)  + $i >= 122) {
                $shifted_alphabet{chr($i)} = chr((($k - 1) % 26)  + $i - 122 + 97);
            } else {
                $shifted_alphabet{chr($i)} = chr(($k % 26)  + $i);
            }
        }
    
        for (my $j = 65; $j <= 90; $j++) {
            if ((($k - 1) % 26)  + $j >= 90) {
                $shifted_alphabet{chr($j)} = chr((($k - 1) % 26)  + $j - 90 + 65);
            } else {
                $shifted_alphabet{chr($j)} = chr(($k % 26)  + $j);            
            }
        }
    
        for (my $h = 0; $h <= scalar(@s); $h++) {
            if (!exists($shifted_alphabet{$s[$h]})) {
                $res .= $s[$h];
            } else {
                $res .= $shifted_alphabet{$s[$h]};
            }
        }
    
        return $res;
    
    }
    
  • + 0 comments
    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
        tmp_list = list('abcdefghijklmnopqrstuvwxyz')
        tmp_list1 = list('ABCDEFGHIJKLMNOPQRSTUVWXYZ')
        s = list(s)
        for i in range(len(s)):
            if s[i] in tmp_list:
                s[i] = tmp_list[(tmp_list.index(s[i])+k)%26]
            elif s[i] in tmp_list1:
                s[i] = tmp_list1[(tmp_list1.index(s[i])+k)%26]
        return ''.join(s)
    
    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

    My answer in Typescript

    const letter = 26;
    const encrypt_char = (char: string, k: number): string => {
        k = k % letter
    
        // ASCII codes
        let ct_min, ct_max = 0
        if (/([a-z])/.test(char)) ct_max = (ct_min = 97) + letter - 1;
        if (/([A-Z])/.test(char)) ct_max = (ct_min = 65) + letter - 1;
    
        // Only encrypt letter (mean a-z or A-Z)
        if (!ct_min || !ct_max) return char;
    
        let code = char.charCodeAt(0)
        let code_encrypted = code + k
    
        if (code_encrypted > ct_max) code_encrypted -= letter
        return String.fromCharCode(code_encrypted)
    }
    function caesarCipher(s: string, k: number): string {
        // doin encrypt every character in string
        return s.split('').map(c => encrypt_char(c, k)).join('')
    }
    
  • + 0 comments

    Simple Java Solution:

    public static String caesarCipher(String s, int k) {
        k = k % 26; // Reduce k to avoid unnecessary shifts
        StringBuilder sb = new StringBuilder();
    
        for (char c : s.toCharArray()) { // Use char directly for simplicity
            if (c >= 'A' && c <= 'Z') { // Uppercase letters
                sb.append((char) ('A' + (c - 'A' + k) % 26));
            } else if (c >= 'a' && c <= 'z') { // Lowercase letters
                sb.append((char) ('a' + (c - 'a' + k) % 26));
            } else { // Non-alphabetic characters
                sb.append(c);
            }
        }
    
        return sb.toString();
    }
    
  • + 0 comments

    Here is my c++ solution, you can watch the explanation here : https://youtu.be/KApgpiqqX20

    #include <bits/stdc++.h>
    using namespace std;
    int main()
    {
       string s;
       int cipher;
       cin >> cipher;
       cin >> s;
       cin >> cipher;
       for(char &c:s) {
           if(isalpha(c)){
               char a = isupper(c)? 'A':'a';
               c = a + (c - a + cipher) % 26;
           }
       }
        cout << s ;
        return 0;
    }