Skip to content
Programmingoneonone
Programmingoneonone
  • Engineering Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
    • 100+ C++ Programs
  • Solutions
    • HackerRank
      • Algorithms Solutions
      • C solutions
      • C++ solutions
      • Java solutions
      • Python solutions
    • Leetcode Solutions
    • HackerEarth Solutions
  • Work with US
Programmingoneonone
Programmingoneonone

HackerRank Caesar Cipher problem solution

YASH PAL, 31 July 202422 January 2026

HackerRank Caesar Cipher problem solution – In this HackerRank Caesar Cipher problem, you need to rotate the string by 3 shifts. and in the case of a rotation by 3, w, x, y, and z would map to z, a, b and c.

Julius Caesar protected his confidential information by encrypting it using a cipher. Caesar’s cipher shifts each letter by a number of letters. If the shift takes you past the end of the alphabet, just rotate back to the front of the alphabet. In the case of a rotation by 3, w, x, y and z would map to z, a, b and c.

Original alphabet:      abcdefghijklmnopqrstuvwxyz
Alphabet rotated +3:    defghijklmnopqrstuvwxyzabc

Function Description

Complete the caesarCipher function in the editor below.

caesarCipher has the following parameter(s):

  • string s: cleartext
  • int k: the alphabet rotation factor

Returns

  • string: the encrypted string

Input Format

The first line contains the integer, n, the length of the unencrypted string.
The second line contains the unencrypted string, s.
The third line contains k, the number of letters to rotate the alphabet by.

HackerRank Caesar Cipher problem solution

HackerRank Caesar Cipher problem solution in Python.

N = int(input())
string = list(input())
addNum = int(input())

length = len(string)
for i in range(length):
	if ord(string[i]) <= ord("Z") and ord(string[i]) >= ord("A"):
		asciiCode = ord(string[i]) + addNum
		while asciiCode > ord("Z"):
			asciiCode = asciiCode - ord("Z") + ord("A") - 1
		string[i] = chr(asciiCode)
	elif ord(string[i]) <= ord("z") and ord(string[i]) >= ord("a"):
		asciiCode = ord(string[i]) + addNum
		while asciiCode > ord("z"):
			asciiCode = asciiCode - ord("z") + ord("a") - 1
		string[i] = chr(asciiCode)

print("".join(string))

Caesar Cipher problem solution in Java.

import java.io.*;
import java.util.*;

public class Solution {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        scanner.nextLine();
        String input = scanner.nextLine();
        int k = scanner.nextInt();
        
        StringBuilder builder = new StringBuilder(input.length());
        for (int i = 0; i < n; i++) {
            char temp = input.charAt(i);
            boolean upperCase = Character.isUpperCase(temp);
            if (Character.isLetter(temp)) {
                temp += k%26;
                if (!Character.isLetter(temp) || (upperCase && !Character.isUpperCase(temp))) {
                    temp -= 26;
                }
            }
            builder.append(temp);
        }
        
        System.out.println(builder.toString());
    }
}

Problem solution in C++.

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


int main() {
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */  
       int n;
       char s[100];
       int k;
    cin >> n;
    for (int i =0; i < n;i++){
        cin>> s[i];
    }
    cin >> k;
    for (int i =0; i<n;i++)
        {
        if((s[i]>='a' && s[i]<='z')){
            char o = s[i]- 'a' + k;
                 o = o %('z'-'a' +1);
                 o+='a';
            cout<<o;
        }
        else if ((s[i]>='A' && s[i]<='Z')){
            char o = s[i] - 'A' + k;
                 o = o %('Z'-'A'+1);
                 o+='A';
            cout<<o;
        }
          else
           cout <<s[i];
                            
    }
    
    return 0;
}

Problem solution in C.

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

int main() {
    int i;
    int n;
    char s[101];
    int k;
    scanf("%d %s %d", &n, s, &k);
    k %= 26;
    for (i=0; i<n; i++) {
        if ((s[i] >= 'A') && (s[i] <= 'Z')) {
            if ((s[i]+k >= 'A') && (s[i]+k <= 'Z'))
                printf("%c", s[i]+k);
            else
                printf("%c", 'A'+(((s[i])+k)-'Z')-1);
        }
        else {
            if ((s[i] >= 'a') && (s[i] <= 'z'))
                if (((s[i])+k >= 'a') && ((s[i])+k <= 'z'))
                    printf("%c", (s[i])+k);
                else
                    printf("%c", 'a'+(((s[i])+k)-'z')-1);
            else
                printf("%c", s[i]);
        }
    }
    return 0;
}

Problem solution in JavaScript.

function processData(input) {
    //Enter your code here
    var lines = input.split("n");
    var L = parseInt(lines[0]);
    var message = lines[1].split("");
    var n = parseInt(lines[2]) % 26;
    var newcode = "";
    
    for (var i = 0; i < L; i++){
        //if the character is a valid letter, increase the charcode by n, and get the letter back, 
        //if it's between 65-90 or 97-122.
        var code = message[i].charCodeAt(0);
        if(65 <= code && code <= 90){
            newcode = code + (code + n > 90 ? n - 26 : n);
            message[i] = String.fromCharCode(newcode);
        }
        else if(97 <= code && code <= 122){
            newcode = code + (code + n > 122 ? n - 26 : n);
            message[i] = String.fromCharCode(newcode);
        }
        else{
            //skip it.
        }
    }
    
    console.log(message.join("").toString());
} 

process.stdin.resume();
process.stdin.setEncoding("ascii");
_input = "";
process.stdin.on("data", function (input) {
    _input += input;
});

process.stdin.on("end", function () {
   processData(_input);
});

Algorithms coding problems solutions AlgorithmsHackerRank

Post navigation

Previous post
Next post

Leave a Reply

Your email address will not be published. Required fields are marked *

Programmingoneonone

We at Programmingoneonone, also known as Programming101 is a learning hub of programming and other related stuff. We provide free learning tutorials/articles related to programming and other technical stuff to people who are eager to learn about it.

Pages

  • About US
  • Contact US
  • Privacy Policy

Practice

  • Java
  • C++
  • C

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2026 Programmingoneonone | WordPress Theme by SuperbThemes