Caesar Cipher Hackerrank problem solution

Caesar Cipher Hackerrank

Julius Caesar protected his confidential information by encrypting it in a cipher. Caesar’s cipher rotated every letter in a string by a fixed number, K, making it unreadable by his enemies. Given a string, S, and a number, K, encrypt S and print the resulting string.

Note: The cipher only encrypts letters; symbols, such as -, remain unencrypted.

Input Format

The first line contains an integer, N, which is the length of the unencrypted string.
The second line contains the unencrypted string, S.
The third line contains the integer encryption key, K, which is the number of letters to rotate.


1<=N<=100 0<=K<=100 S is a valid ASCII string and doesn't contain any spaces. Output Format

For each test case, print the encoded string.

Sample Input


Sample Output


Each unencrypted letter is replaced with the letter occurring K spaces after it when listed alphabetically. Think of the alphabet as being both case-sensitive and circular; if K rotates past the end of the alphabet, it loops back to the beginning (i.e.: the letter after z is a, and the letter after Z is A).

Selected Examples:
m (ASCII 109) becomes o (ASCII 111).
i (ASCII 105) becomes k (ASCII 107).
– remains the same, as symbols are not encoded.
O (ASCII 79) becomes Q (ASCII 81).
z (ASCII 122) becomes b (ASCII 98); because is the last letter of the alphabet, a (ASCII 97) is the next letter after it in lower-case rotation.


To encrypt the string, we need to rotate the alphabets in the string by a number k. If k is a multiple of 26, then rotating the alphabets in the string by k has no effect on the string. Rotation by k is same as rotation by k+26. So by taking modulo of k with 26, we get the number of times we need to rotate the alphabets of the string. To rotate the alphabets in a string by a value k, we add k to the character. If this value exceeds the range of the alphabets, we need to wrap it back. The range of uppercase characters is from 65 (‘A’) to 90 (‘Z’). The range of lowercase characters is from 97 (‘a’) to 122 (‘z’).
Example: For the string : “middle-Outz” and k=2
We add 2 to ‘m’. ‘m’ becomes ‘o’. This value is within the ascii range so we don’t need to wrap it. ‘-‘ remains unaltered. ‘z’ on adding 2 becomes 124. This value lies outside the range of lowercase characters. We need to wrap this value. By taking the modulo of this value with 122, and adding this value to 96(‘a’-1) we get the rotated character.
For lowercase characters,
// Let char c = s[i] be a lowercase character in the string.
k = k % 26;
c += k;
if(c > 122) {
c = 96 + (c % 122);

#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <ctype.h>
#include <limits.h>
#include <stdbool.h>

int main(){
    int n; 
    char* s = (char *)malloc(10240 * sizeof(char));
    int k,x; 
    for(int i=0; i<=strlen(s);i++){
        if( isalpha(s[i]) ){
            if( isupper(s[i]) )
           int a=(int)s[i];
               a= x+((a+k-x)%26);
           char c=a;
    return 0;
#include <iostream>
#include <string>

using namespace std;

int main() {
    int n;
    string s;
    cin >> n;
    cin >> s;
    int k;
    cin >> k;

    k %= 26;
    for (int i = 0;i < n; i++) {
        int c = s[i];
        if (c >= 'a' && c <= 'z') { // Lowercase characters
            c += k;
            if (c > 'z') { // C exceeds the ascii range of lowercase characters.
               c = 96 + (c % 122); // wrapping from z to a
        } else if(c >= 'A' && c <= 'Z') { // Uppercase characters
            c += k;
            if(c > 'Z') { // C exceeds the ascii range of uppercase characters.
                c = 64 + (c % 90); //wrapping from Z to A
        cout << (char)c;
    cout << endl;
    return 0;
n = input()
s = list(raw_input())
k = input() % 26
temp = map(lambda x: ord(x), s)
for i in range(len(s)):
    if 65 <= temp[i] <= 90:
        temp[i] = 65 + ((temp[i] - 65) + k) % 26
    elif 97 <= temp[i] <= 122:
        temp[i] = 97 + ((temp[i] - 97) + k) % 26
print "".join(map(chr, temp))

A web developer(Front end and Back end), and DBA at Currently working as Salesforce Developer @ Tech Matrix IT Consulting Private Limited. Check me

Leave a reply:

Your email address will not be published.