Sort by

recency

|

1280 Discussions

|

  • + 0 comments

    include

    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; } `

  • + 0 comments

    Javascript

    const letter='abcdefghijklmnopqrstuvwxyz';
        const arr=letter.split('');
        let final=''"
        for(let i=1;i<=k;i++){
            let shi=arr.shift();
            arr.push(shi);
        }
        s.split('').forEach(val=>{
            if(val.match(/[^a-zA-Z]/g)){
                final+=val
                return
            }
            if(val.match(/[A-Z]/g)){
                let index=letter.split('').indexOf(val.toLowerCase());
                let value=arr.at(index).toUpperCase();
                final+=value
                return
            }
            let index=letter.split('').indexOf(val);
            let value=arr.at(index)
          final+=value
        })
        return final
    
  • + 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;
    }
    
  • + 0 comments
    def caesarCipher(s, k):
        # Write your code here
        new_string = ""
        for char in s:
            if char.isalpha():
                min_limit = ord("a") if char.islower() else ord("A")
                max_limit = ord("z") if char.islower() else ord("Z")
                pos = ord(char) + (k % 26)
    
                if min_limit <= pos <= max_limit:
                    new_string += chr(pos)
                elif pos > max_limit:
                    new_string += chr((pos - max_limit) + (min_limit - 1))
            else:
                new_string += char
    
        return new_string
    
  • + 0 comments

    C++ solution:

    string caesarCipher(string s, int k) {
        string alphabets = "abcdefghijklmnopqrstuvwxyz" ;
        for(int i=0;i<s.length();i++){
            char c = tolower(s[i]);
            int pos = alphabets.find(c);
            if(pos == -1){
                continue;
            }
            if(isupper(s[i])){
                char newChar = alphabets[(pos+k)%26];
                s[i] = toupper(newChar);
            }else{
                s[i] = alphabets[(pos+k)%26];
            }; 
        }
        return s;
    }