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

    }