Practical No-1(b)
Aim: Write a program to implement Modified Caesar
Cipher Substitution Technique.
Program:
import java.io.*;
import java.util.*;
import java.lang.String;
class ModifiedCaesar
{
public static void main(String[]
args)throws Exception
{
try
{
int key;
String s;
Scanner in = new Scanner(System.in);
System.out.println("Enter a string
to encrypt");
s = in.nextLine();
System.out.println("Enter a key");
key=in.nextInt();
char arr[]=s.toCharArray();
String str=" ";
for (int i=0;i < s.length(); i++)
{
int helper=s.charAt(i);
helper=(helper+key);
if (helper>'Z'||helper>'z') helper =(helper-26);
if (helper<'A'||helper<'a') helper=(helper+26);
str= str+(char)helper;
}
System.out.println("Cipher
text: "+ str);
}
catch(Exception
e)
{
System.out.println("Error"+e);
}
}
}
Output:
Enter a String to encrypt : hello
Enter a Key : 4
Cipher text : limmp
No comments:
Post a Comment