[Question 3] ISC 2017 Computer Practical Paper Solved – Caesar Cipher
ISC 2017 Caesar Cipher ROT13 program solved with algorithm, explanation, Java program and Python code.
Click here to download the complete ISC 2017 Computer Science Paper 2 (Practical).
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.
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 qngn.
Example 3
INPUT : You
OUTPUT : INVALID LENGTH
Algorithm:
Step 1: Start.
Step 2: Input the sentence.
Step 3: Find the length of the sentence.
Step 4: If the length is less than 4 or greater than 99, display INVALID LENGTH and stop.
Step 5: Scan each character of the sentence.
Step 6: If the character is an alphabet, shift it 13 places ahead.
Step 7: If the shifted character crosses Z or z, subtract 26 to wrap around the alphabet.
Step 8: Leave non-alphabetic characters unchanged.
Step 9: Display the encrypted text.
Step 10: Stop.
Explanation:
Caesar Cipher in this question uses ROT13, which means each alphabet is replaced by the letter 13 positions ahead of it. Since the English alphabet has 26 letters, shifting by 13 twice gives the original text back. The program therefore needs to process only alphabetic characters. Spaces, punctuation marks and other non-letter characters are copied as they are so that the structure of the sentence remains readable.
The program first checks the sentence length because the question allows only a length from 4 to 99 characters. After validation, it scans the sentence character by character. For each letter, 13 is added to its character code. If an uppercase character moves beyond Z or a lowercase character moves beyond z, the program subtracts 26 to wrap it back to the beginning of the alphabet. The converted characters are added to the answer string, and finally the cipher text is printed. This implementation preserves uppercase and lowercase letters separately while keeping punctuation unchanged.
For a dry run, the letter E shifts 13 places to R, while n shifts beyond z and wraps around to a. A space or full stop is copied directly to the result. This character-by-character handling is necessary because the input is a sentence, not a single word. The output keeps the same spacing and punctuation, but every alphabetic character is replaced by its ROT13 equivalent.
Programming Code:
/**
* The class CaesarCipher_ISC2017 inputs a sentence and encrypts it by shifting
* every alphabet 13 places ahead in a circular fashion
* @author : www.guideforschool.com
* @Program Type : BlueJ Program - Java
* @Question Year : ISC Practical 2017 Question 3
*/
import java.util.*;
class CaesarCipher_ISC2017
{
void rot13(String w)
{
char ch;
int a = 0;
String ans = "";
for(int i = 0; i<w.length(); i++)
{
ch = w.charAt(i);
if(Character.isLetter(ch))
{
a = ch + 13;
if((Character.isUpperCase(ch) && a>90) || (Character.isLowerCase(ch) && a>122))
{
a = a - 26;
}
ch = (char)a;
}
ans = ans + ch;
}
System.out.println("OUTPUT : The cipher text is :\n"+ans);
}
public static void main(String args[])
{
CaesarCipher_ISC2017 ob = new CaesarCipher_ISC2017();
Scanner sc = new Scanner(System.in);
System.out.print("Enter a sentence : ");
String s = sc.nextLine();
int L = s.length();
if(L<4 || L>99)
{
System.out.println("INVALID LENGTH");
}
else
{
ob.rot13(s);
}
}
}Equivalent Python Program:
def rot13(text):
result = ""
for ch in text:
if ch.isalpha():
code = ord(ch) + 13
if ch.isupper() and code > ord('Z'):
code -= 26
elif ch.islower() and code > ord('z'):
code -= 26
result += chr(code)
else:
result += ch
return result
sentence = input("Enter a sentence : ")
length = len(sentence)
if length < 4 or length > 99:
print("INVALID LENGTH")
else:
print("OUTPUT : The cipher text is :")
print(rot13(sentence))Output:
Enter a sentence : Encryption helps to secure data.
OUTPUT : The cipher text is :
Rapelcgvba urycf gb frpher qngn.
Leave a Reply
Your email address will not be published. Comments are reviewed before appearing publicly.