Encryption Program ISC Computer Science 2011 Practical

Encryption is a technique of coding messages to maintain their secrecy. A string array of size 'n' where 'n' is greater than 1 and less than 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 inputted 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 character. Also change the sentence of the even rows by storing the sentence in reverse order.

Display the encrypted sentences as per the sample data given below:

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

Example 1:
INPUT:
n = 4
IT IS CLOUDY.
IT MAY RAIN.
THE WEATHER IS FINE.
IT IS COOL.
OUTPUT:
KV KU ENQWFA.
RAIN MAY IT.
VJG TGCVJGT KU HKPG.
COOL IS IT.

Example 2:
INPUT:
n = 13
OUTPUT:
INVALID ENTRY

import java.util.Scanner;
class Encryption{
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        System.out.print("n = ");
        int n = Integer.parseInt(in.nextLine());
        if(n < 2 || n > 9){
            System.out.println("INVALID ENTRY");
            return;
        }
        String a[] = new String[n];
        System.out.println("Enter " + n + " sentences:");
        for(int i = 0; i < a.length; i++){
            char last = '\u0000';
            do{
                a[i] = in.nextLine().toUpperCase();
                last = a[i].charAt(a[i].length() - 1);
                if(".?!".indexOf(last) == -1)
                    System.out.println("Enter a valid sentence!");
            }while(".?!".indexOf(last) == -1);
        }
        for(int i = 0; i < a.length; i++){
            if(i % 2 == 0){
                String encrypt = "";
                for(int j = 0; j < a[i].length(); j++){
                    char ch = a[i].charAt(j);
                    if(Character.isLetter(ch)){
                        if(ch + 2 <= 90)
                            encrypt += (char)(ch + 2);
                        else if(ch == 'Y')
                            encrypt += 'A';
                        else
                            encrypt += 'B';
                    }
                    else
                        encrypt += ch;
                }
                a[i] = encrypt;
            }
            else{
                String s = "";
                String w = "";
                for(int j = 0; j < a[i].length(); j++){
                    char ch = a[i].charAt(j);
                    if(!Character.isLetterOrDigit(ch)){
                        s = w + " " + s;
                        w = "";
                    }
                    else
                        w += ch;
                }
                a[i] = s.trim() + a[i].charAt(a[i].length() - 1);
            }
        }
        for(int i = 0; i < a.length; i++)
            System.out.println(a[i]);
    }
}

Comments

Popular posts from this blog

Encrypt Program ISC Specimen 2023 Theory

No Repeat Program ISC Computer Science 2022 Semester 2 Theory

Bank Inheritance Program ISC Specimen 2023 Theory