ISC practical papers › ISC 2017 › Question 3
Caesar Cipher (ROT-13)
ISC 2017 Computer Science Practical, Question 3 · Strings
The question
Caesar Cipher is an encryption technique which is implemented as ROT13 (‘rotate by 13 places’). It is a simple letter substitution cipher that replaces a letter with the letter 13 places after it in the alphabets, with the other characters remaining unchanged.
ROT13
+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+ | A/a | B/b | C/c | D/d | E/e | F/f | G/g | H/h | I/i | J/j | K/k | L/l | M/m | +-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+ | N/n | O/o | P/p | Q/q | R/r | S/s | T/t | U/v | V/v | W/w | X/x | Y/y | Z/z | +-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+
Write a program to accept a plain text of length L, where L must be greater than 3 and less than 100.
Encrypt the text if valid as per the Caesar Cipher. Test your program with the sample data and some random data:
Example 1
INPUT: Hello! How are you?
OUTPUT: The cipher text is:
Uryyb? Ubj ner lbh?
Example 2
INPUT: Encryption helps to secure data.
OUTPUT: The cipher text is:
Rapelcgvba Urycf gb frpher gngn.
Example 3 INPUT: You OUTPUT: INVALID LENGTH
Question: ISC 2017 Computer Science Paper 2 (Practical), Question 3, set by the Council for the Indian School Certificate Examinations (CISCE). Solution and video: Vinay Singh.
Java solution
import java.util.*;
class CaesarCipher{
private String L,msg;
public CaesarCipher(String L){
this.L=L;
}
public void encode(){
msg="";
for( int position=0; position<L.length();position++){
msg+=rot13(L.charAt(position));
}
}
private static char rot13(char ch){
if(Character.isLetter(ch)){
if(ch>='A' && ch<='M'){
ch= (char)(ch+13);
}else if(ch>='N' && ch <='Z'){
ch= (char)(ch-13);
}else if(ch>='a' && ch<='m'){
ch= (char)(ch+13);
}else if(ch>='n' && ch <='z'){
ch= (char)(ch-13);
}
}
return ch;
}
public String toString(){
return "\t"+msg;
}
}
public class ISC2017Q3 {
public static void main(String[] args) {
Scanner in = new Scanner( System.in );
System.out.print("INPUT:\t");
String L=in.nextLine();
if(L.length()>3 && L.length()<100){
System.out.println("OUTPUT:\tThe cipher text is:");
CaesarCipher cipher=new CaesarCipher(L);
cipher.encode();
System.out.println(cipher);
}else{
System.out.println("OUTPUT:\tINVALID LENGTH");
}
in.close();
}
}
All these solutions in one free book
Pragmatic Solutions to ISC Computer Science Practical Problems collects every program from 2004 to 2021 in a 202-page PDF.