Categories

Encryption Program ISC 2011 Practical

Encryption is a technique of coding messages to maintain their secrecy. A string array of size ‘n’ where 1 < n < 10, stores single sentences (each sentence ends with a full stop) in each row of the array.

Write a program to accept the size of the array. Display an appropriate message if the size is not satisfying the given condition. Define a string array of the accepted size and fill it with sentences row-wise. Change the sentence of the odd rows with an encryption of two characters ahead of the original characters. Also change the sentence of the even rows by storing the sentence in reverse order of their words. Display the encrypted sentences as per the sample data given below.

Test your program on the sample data and some random data.

SAMPLE DATA:
INPUT:
n = 4
IT IS CLOUDY.
IT MAY RAIN.
THE WEATHER IS FINE.
IT IS COOL.
OUTPUT:
KV KU ENQWFA.
RAIN MAY IT.
VJG YGCVJGT KU HKPG.
COOL IS IT.

INPUT:
n = 13
OUTPUT:
INVALID INPUT

``````import java.io.*;
import java.util.StringTokenizer;
class Encryption{
public static void main(String args[])throws IOException{
System.out.print("n = ");
if(n <= 1 || n >= 10){
System.out.println("INVALID INPUT");
return;
}
String a[] = new String[n];
System.out.println("Enter sentences:");
for(int i = 0; i < a.length; i++){
a[i] = a[i].trim();
if(a[i].charAt(a[i].length() - 1) != '.'){
System.out.println("INVALID INPUT");
return;
}
if((i + 1) % 2 == 1)
a[i] = encode(a[i]);
else
a[i] = reverse(a[i]);
}
for(int i = 0; i < a.length; i++)
System.out.println(a[i]);
}
public static String encode(String s){
String t = "";
for(int i = 0; i < s.length(); i++){
char ch = s.charAt(i);
if(Character.isLetter(ch)){
if(ch == 'Y')
t += 'A';
else if(ch == 'Z')
t += 'B';
else
t += (char)(ch + 2);
}
else
t += ch;
}
return t;
}
public static String reverse(String s){
String t = "";
StringTokenizer st = new StringTokenizer(s, " .");
int count = st.countTokens();
for(int i = 1; i <= count; i++){
String w = st.nextToken();
t = w + " " + t;
}
t = t.trim();
return t + ".";
}
}``````

This site uses Akismet to reduce spam. Learn how your comment data is processed.