HackerRank 'Caesar Cipher' Solution

by Srikant Padala on March 25, 2018, 12:59 pm


Explanation



Caesar Cipher Problem Statement

Video

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;

string getEncrypted(string &str,int k) {
    k%=26;
    for(int i=0; i < str.size(); i++) {
        if(isalpha(str[i])) {
            if(isupper(str[i])) {
                if(str[i] + k > 'Z') str[i] -= 26;
            }
            else {
                if(str[i] + k > 'z') str[i] -= 26;
            }
            str[i] += k;
        }
    }
    return str;
}

int main(){
    int n;
    cin >> n;
    string s;
    cin >> s;
    int k;
    cin >> k;
    cout << getEncrypted(s,k);
    return 0;
}

Coming Soon.