Caesar Cipher ISC Computer Science 2017 Practical
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.
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/u | 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 qngn.
Example 3:
INPUT: You
OUTPUT:
INVALID LENGTH
import java.util.Scanner;
class CaesarCipher{
public static void main(String args[]){
Scanner in = new Scanner(System.in);
System.out.print("Enter the string: ");
String s = in.nextLine();
if(s.length() < 4 || s.length() > 99){
System.out.println("INVALID LENGTH");
return;
}
String c = "";
for(int i = 0; i < s.length(); i++){
char ch = s.charAt(i);
if((ch >= 'A' && ch <= 'M') || (ch >= 'a' && ch <= 'm'))
c += (char)(ch + 13);
else if((ch >= 'N' && ch <= 'Z') || (ch >= 'n' && ch <= 'z'))
c += (char)(ch - 13);
else
c += ch;
}
System.out.println(c);
}
}
class CaesarCipher{
public static void main(String args[]){
Scanner in = new Scanner(System.in);
System.out.print("Enter the string: ");
String s = in.nextLine();
if(s.length() < 4 || s.length() > 99){
System.out.println("INVALID LENGTH");
return;
}
String c = "";
for(int i = 0; i < s.length(); i++){
char ch = s.charAt(i);
if((ch >= 'A' && ch <= 'M') || (ch >= 'a' && ch <= 'm'))
c += (char)(ch + 13);
else if((ch >= 'N' && ch <= 'Z') || (ch >= 'n' && ch <= 'z'))
c += (char)(ch - 13);
else
c += ch;
}
System.out.println(c);
}
}
Comments
Post a Comment