Code with Vinay

ISC practical papers › ISC 2011 › Question 2

Encryption Problem

ISC 2011 Computer Science Practical, Question 2 · Strings

The question

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.

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 ENTRY

Question: ISC 2011 Computer Science Paper 2 (Practical), Question 2, set by the Council for the Indian School Certificate Examinations (CISCE). Solution and video: Vinay Singh.

Java solution

ISC2011Q2.java Download GitHub
import java.util.*;

class Encrypt {
    private String sentence[], answer;

    public Encrypt(String sentence[]) {
        this.sentence = sentence.clone();
        answer = "";
        encrypt();
    }

    private void encrypt() {
        int lineCount = 0;
        char ch;
        String output, words[];
        for (String line : sentence) {
            output = "";
            lineCount++;
            line = line.substring(0, line.length() - 1);
            if (lineCount % 2 == 1) {
                // odd line
                for (int position = 0; position < line.length(); position++) {
                    ch = line.charAt(position);
                    if (Character.isLetter(ch)) {
                        ch = (char) (ch + 2);
                        if (ch > 'Z')
                            ch = (char) (ch - 26);
                        
                    }
                    output += ch;
                }

            } else {
                // even line
                words = line.split(" ");
                for (String word : words) {
                    output = word + " " + output;
                }
            }
            output=output.trim();
            output += ".";
            answer += output + "\n";
        }
    }

    public String toString() {
        return answer;
    }
}

class ISC2011Q2 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.print("INPUT:\tn = ");
        int n = in.nextInt();
        in.nextLine();
        if (n > 1 && n < 10) {
            String sentence[] = new String[n];
            for (int i = 0; i < n; i++) {
                sentence[i] = in.nextLine();
            }
            Encrypt obj = new Encrypt(sentence);
            System.out.println("OUTPUT:");
            System.out.println(obj);
        } else {
            System.out.println("OUTPUT\t:INVALID ENTRY");
        }
        in.close();
    }
}

All these solutions in one free book

Pragmatic Solutions to ISC Computer Science Practical Problems collects every program from 2004 to 2021 in a 202-page PDF.

Get the free PDF ₹0 ISC 2027 mock papers