Code With Vinay Logo Code with Vinay

ISC practical papers › 2027 masterclass › Episode 3

Strings, split() & Caesar Ciphers

ISC 2027 Computer Science Practical masterclass · Episode 3 of 4 · Vinay Singh

The problem

Accept one sentence that ends with a full stop, a question mark or an exclamation mark. Convert it to uppercase. Encrypt each word with a plus-two circular Caesar cipher (A becomes C, Y becomes A, Z becomes B). Sort the encrypted words by length, and alphabetically when two words have the same length. Rebuild the sentence with single spaces and the original terminator, then count the vowels and consonants.

Rules and messages

  • If the sentence is empty, or does not end with ., ? or !, print INVALID INPUT.
  • If the sentence has fewer than two words, print INVALID INPUT.

Algorithm

Written step by step, the way the ISC practical asks for it, and matched to the program below. How the algorithm is marked

  1. Step 1: Start.
  2. Step 2: Input the sentence raw and trim the spaces at both ends.
  3. Step 3: If raw is empty or does not end with ., ? or !, display INVALID INPUT and stop.
  4. Step 4: Store the last character in terminator. Take the rest of the sentence, trim it, and convert it to uppercase in text.
  5. Step 5: Split text into the array words, using the regular expression \s+ (one or more spaces).
  6. Step 6: If words has fewer than two words, display INVALID INPUT and stop.
  7. Step 7: Count the vowels and consonants in text.
  8. Step 8: Encrypt each word with the plus-two circular Caesar cipher.
  9. Step 9: Sort the encrypted words by length, then alphabetically (bubble sort).
  10. Step 10: Join the words with single spaces and add the terminator at the end.
  11. Step 11: Display the original sentence, the transformed sentence, the vowel count and the consonant count.
  12. Step 12: Stop.

Variable description

NameData typePurpose
rawStringthe sentence as typed
terminatorcharthe last character of raw
textStringthe sentence without the terminator, trimmed and in uppercase
wordsString[]the words of text
encryptedString[]the encrypted words
vowelCountintnumber of vowels in text
consonantCountintnumber of consonants in text
lenA, lenBintlengths of two adjacent words while sorting
shouldSwapbooleantrue when two adjacent words must swap
tempStringtemporary store while swapping
resultStringthe rebuilt transformed sentence
i, jintloop counters

Methods in the program

Java solution

StringCipherSort.java Download
import java.util.Scanner;

/**
 * ISC Computer Science Practical Examination
 * Topic: String Manipulation, Tokenization, Caesar Cipher & Custom Sorting
 * Class: StringCipherSort
 */
public class StringCipherSort {

    // Helper method to encrypt a single word using Caesar Cipher (+2 shift)
    public static String encryptWord(String word) {
        String enc = "";
        for (int i = 0; i < word.length(); i++) {
            char ch = word.charAt(i);
            if (ch >= 'A' && ch <= 'Z') {
                // Circular shift: (ch - 'A' + 2) % 26 + 'A'
                char shifted = (char) (((ch - 'A' + 2) % 26) + 'A');
                enc += shifted;
            } else {
                enc += ch;
            }
        }
        return enc;
    }

    // Helper method to check if a character is a vowel
    public static boolean isVowel(char ch) {
        return ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U';
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.println("ENTER SENTENCE:");
        String raw = in.nextLine().trim();

        // 1. Validation Gate: Must not be empty and must terminate with ., ?, or !
        if (raw.isEmpty()) {
            System.out.println("INVALID INPUT");
            return;
        }

        char terminator = raw.charAt(raw.length() - 1);
        if (terminator != '.' && terminator != '?' && terminator != '!') {
            System.out.println("INVALID INPUT");
            return;
        }

        // 2. Strip terminator and convert to UPPERCASE
        String text = raw.substring(0, raw.length() - 1).trim().toUpperCase();
        if (text.isEmpty()) {
            System.out.println("INVALID INPUT");
            return;
        }

        // 3. Robust Tokenization using regex (collapses multiple consecutive spaces)
        String[] words = text.split("\\s+");
        if (words.length < 2) {
            System.out.println("INVALID INPUT");
            return;
        }

        // 4. Count Vowels and Consonants in original text
        int vowelCount = 0;
        int consonantCount = 0;
        for (int i = 0; i < text.length(); i++) {
            char ch = text.charAt(i);
            if (ch >= 'A' && ch <= 'Z') {
                if (isVowel(ch)) {
                    vowelCount++;
                } else {
                    consonantCount++;
                }
            }
        }

        // 5. Caesar Cipher Encryption (+2 circular shift)
        String[] encrypted = new String[words.length];
        for (int i = 0; i < words.length; i++) {
            encrypted[i] = encryptWord(words[i]);
        }

        // 6. Custom Sorting: Length Priority, Alphabetical Tie-Breaker (Bubble Sort)
        for (int i = 0; i < encrypted.length - 1; i++) {
            for (int j = 0; j < encrypted.length - 1 - i; j++) {
                int lenA = encrypted[j].length();
                int lenB = encrypted[j + 1].length();
                boolean shouldSwap = false;

                if (lenA > lenB) {
                    shouldSwap = true;
                } else if (lenA == lenB) {
                    if (encrypted[j].compareTo(encrypted[j + 1]) > 0) {
                        shouldSwap = true;
                    }
                }

                if (shouldSwap) {
                    String temp = encrypted[j];
                    encrypted[j] = encrypted[j + 1];
                    encrypted[j + 1] = temp;
                }
            }
        }

        // 7. Reassemble transformed sentence with original terminator
        String result = "";
        for (int i = 0; i < encrypted.length; i++) {
            result += encrypted[i];
            if (i < encrypted.length - 1) {
                result += " ";
            }
        }
        result += terminator;

        // Display Outputs
        System.out.println("\nORIGINAL SENTENCE:");
        System.out.println(raw.toUpperCase());

        System.out.println("\nTRANSFORMED SENTENCE:");
        System.out.println(result);

        System.out.println("\nVOWELS: " + vowelCount);
        System.out.println("CONSONANTS: " + consonantCount);
    }
}

Sample runs

Each run uses the program above. The output is copied from the terminal.

Sample sentence

Input, typed in this order

Java is fun.

Output

ENTER SENTENCE:

ORIGINAL SENTENCE:
JAVA IS FUN.

TRANSFORMED SENTENCE:
KU HWP LCXC.

VOWELS: 4
CONSONANTS: 5

Invalid: only one word

Input, typed in this order

HELLO.

Output

ENTER SENTENCE:
INVALID INPUT

Invalid: no terminator

Input, typed in this order

NO TERMINATOR

Output

ENTER SENTENCE:
INVALID INPUT