• + 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;
    }