Sort by

recency

|

1288 Discussions

|

  • + 0 comments

    c++

    string caesarCipher(string s, int k) {
        string caesar;
        k = k % 26; 
        
        for (char c : s) {
            if (isupper(c)) {
                caesar += (char)(((c - 'A' + k) % 26) + 'A');
            } else if (islower(c)) {
                caesar += (char)(((c - 'a' + k) % 26) + 'a');
            } else {
                caesar += c;
            }
        }
        
        return caesar;
    }
    
  • + 0 comments

    here is my simple C++ solution // // //

    string caesarCipher(string s, int k) { string res(""); int ch = 0;

    for (auto c : s)
    {
        if (isalpha(c))
        {
            int ch = int(c);
            ch += k;
    
            /* check where we are in relation to 1 -26 , taking into
            account of any Caps letter
            */
            int curr_index = isupper(c) ? (int(c) -65) : int(c) - 97;
    
            /* by how much are we going to be out of bound*/
            bool out_of_boundOffset = (k + curr_index > 25);
    
            if ( out_of_boundOffset )
                ch = isupper(c) ? (65 + (k + curr_index) % 26) : (97 + (k + curr_index) % 26);
    
            res.push_back(char(ch));
        }
        else {
            res.push_back(c);
        }
    }
    return res;
    

    }

  • + 0 comments

    My python solution

    s2 = ''
        for ch in s :
            if ch.isalpha():
                t = ord(ch)
                if 96 <= t <= 122 :
                    t += k
                    while t > 122 :
                        t -= 26
                elif 65 <= t <= 90 :
                    t += k 
                    while t > 90 :
                        t -= 26
                s2 += chr(t)
            else :
                s2 += ch
        return s2
    
  • + 0 comments
    static Map<Character, Integer> smallcharintmap = new HashMap<>();
    static Map<Integer, Character> smallintcharmap = new HashMap<>();
    
    static Map<Character, Integer> capscharintmap = new HashMap<>();
    static Map<Integer, Character> capsintcharmap = new HashMap<>();
    
    static {
        char smallstart = 'a';
        char capstart = 'A';
    
        for(int i=1;i<=26;i++) {
            smallcharintmap.put(smallstart, i);
            smallintcharmap.put(i, smallstart);
            smallstart++;
    
            capscharintmap.put(capstart, i);
            capsintcharmap.put(i, capstart);
            capstart++;
        }
    }
    
    public static String caesarCipher(String str, int k) {
        StringBuilder resultBuilder = new StringBuilder();
        for(int i = 0; i < str.length(); i++) {
            Character ch = str.charAt(i);
            if(Character.isAlphabetic(ch)) {
                if(Character.isLowerCase(ch)) {
                    int key = (smallcharintmap.get(ch) + k)%26;
                    resultBuilder.append(smallintcharmap.get(key == 0 ? 26 : key));
                } else if(Character.isUpperCase(ch)) {
                    int key = (capscharintmap.get(ch) + k)%26;
                    resultBuilder.append(capsintcharmap.get(key == 0? 26 : key));
                }
            } else {
                resultBuilder.append(ch);
            }
        }
        return resultBuilder.toString();
    }
    
  • + 0 comments
    int base;
    for(int i=0; i<s.size(); i++){
            if(isalpha(s[i])){
                    if(islower(s[i])){
                            base = 'a';
                    }else{
                            base = 'A';
                    }
                    s[i] = ((s[i] - base + k) % 26) + base;
            }
    }
    return s;
    

    }