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
- Step 1: Start.
- Step 2: Input the sentence raw and trim the spaces at both ends.
- Step 3: If raw is empty or does not end with ., ? or !, display INVALID INPUT and stop.
- Step 4: Store the last character in terminator. Take the rest of the sentence, trim it, and convert it to uppercase in text.
- Step 5: Split text into the array words, using the regular expression \s+ (one or more spaces).
- Step 6: If words has fewer than two words, display INVALID INPUT and stop.
- Step 7: Count the vowels and consonants in text.
- Step 8: Encrypt each word with the plus-two circular Caesar cipher.
- Step 9: Sort the encrypted words by length, then alphabetically (bubble sort).
- Step 10: Join the words with single spaces and add the terminator at the end.
- Step 11: Display the original sentence, the transformed sentence, the vowel count and the consonant count.
- Step 12: Stop.
Variable description
| Name | Data type | Purpose |
|---|---|---|
raw | String | the sentence as typed |
terminator | char | the last character of raw |
text | String | the sentence without the terminator, trimmed and in uppercase |
words | String[] | the words of text |
encrypted | String[] | the encrypted words |
vowelCount | int | number of vowels in text |
consonantCount | int | number of consonants in text |
lenA, lenB | int | lengths of two adjacent words while sorting |
shouldSwap | boolean | true when two adjacent words must swap |
temp | String | temporary store while swapping |
result | String | the rebuilt transformed sentence |
i, j | int | loop counters |
Methods in the program
- encryptWord(String word): returns the word with each letter shifted two places forward, wrapping Y to A and Z to B.
- isVowel(char ch): returns true for A, E, I, O and U (uppercase letters).
Java solution
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