In ICSE Java, a Magic String (or Magic Word) is a word containing at least one pair of consecutive alphabetical letters side by side, such as 'A' followed immediately by 'B' in "CABINET" or 'O' and 'P' in "TROPHY". Convert the input word to uppercase, iterate i from 0 to len - 2, and test (ch2 - ch1) == 1 && Character.isLetter(ch1). Because Java characters store ASCII codes, ch2 == ch1 + 1 detects consecutive alphabetical neighbors instantly.
- 1. How ASCII Arithmetic Detects Consecutive Letters
- 2. Dry Run on "CABINET" vs "COMPUTER"
- 3. Differentiating Adjacent-Consecutive vs Whole-Word Alphabetical Magic Strings
- 4. MagicStringCheck.java (BlueJ / ICSE Section B)
- 5. CISCE Variable Description Table (VDT) & Dry Run
- 6. Common Mistakes & Top Scorer Tips
- 7. Interactive Concept Check Quiz
- 8. Frequently Asked Questions
1. How ASCII Arithmetic Detects Consecutive Letters
In Java, char is an unsigned 16-bit integer type holding Unicode/ASCII values—uppercase 'A' through 'Z' occupy contiguous codes 65 through 90. Subtracting two neighboring characters (ch2 - ch1) computes the exact alphabetical step between them without needing lookup tables or indexOf("ABCDEFGHIJKLMNOPQRSTUVWXYZ"). When word is normalized with toUpperCase(), 'A' followed by 'B' gives 66 - 65 == 1, and 'X' followed by 'Y' gives 89 - 88 == 1. Watch out for one subtle trap: digit characters '0' through '9' also sit sequentially at ASCII 48 to 57, so '1' followed by '2' would satisfy ch2 - ch1 == 1 unless you also check Character.isLetter(ch1) && Character.isLetter(ch2).
- Consecutive letters in forward alphabetical order always have (int) ch2 - (int) ch1 == 1.
- Uppercase normalization (toUpperCase()) ensures mixed-case pairs like 'a' + 'B' (66 - 97 = -31) are not missed.
- Use a boolean isMagic = false; flag and break; immediately once a valid pair is found.
2. Dry Run on "CABINET" vs "COMPUTER"
Test the flag logic on "CABINET" (length 7). At i = 0, ch1 is 'C' (67) and ch2 is 'A' (65); 65 - 67 = -2, so isMagic remains false. At i = 1, ch1 is 'A' (65) and ch2 is 'B' (66); 66 - 65 == 1 and both are letters, so isMagic flips to true and break exits early without wasting cycles on "INET". By contrast, "COMPUTER" scans CO (12), OM (-2), MP (3), PU (5), UT (-1), TE (-15), and ER (13). None equal 1, so isMagic stays false, printing "Not a Magic String". Notice 'U' followed by 'T' gives -1—reverse order is excluded in standard definitions.
- Forward consecutive pairs like AB, DE, MN, OP, and ST all satisfy ch2 - ch1 == 1.
- Do not wrap 'Z' to 'A' unless a school worksheet explicitly requests cyclic wrap-around.
- Never print the verdict inside the loop—check isMagic once after the loop finishes.
3. Differentiating Adjacent-Consecutive vs Whole-Word Alphabetical Magic Strings
Before writing code in an exam hall, verify which variation of Magic String your question paper defines. The classic ICSE definition ("AB", "CABINET", "STUMP") requires at least one consecutive pair anywhere in the word, checked via ch2 - ch1 == 1 with break. A second school-level variant counts how many consecutive pairs exist in a sentence ("FIRST" has RS and ST, count 2), which swaps the boolean flag for an int count variable. A third rare variant defines a strictly consecutive word where every pair must satisfy ch2 - ch1 == 1 (such as "ABCD"), which starts isMagic = true and flips to false on any mismatch.
- Read the sample test cases in the exam prompt carefully ("COMPUTER" vs "CABINET" confirms the rule).
- Use sc.next() if only a single word is guaranteed, or sc.nextLine().trim() to stay safe.
- Document the ASCII difference logic clearly in the Variable Description Table.
4. MagicStringCheck.java (BlueJ / ICSE Section B)
import java.util.*;
public class MagicStringCheck {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a word: ");
String word = sc.next().toUpperCase();
int len = word.length();
boolean isMagic = false;
// Check adjacent characters for consecutive alphabetical ASCII codes
for (int i = 0; i < len - 1; i++) {
char ch1 = word.charAt(i);
char ch2 = word.charAt(i + 1);
if (Character.isLetter(ch1) && Character.isLetter(ch2) && (ch2 - ch1) == 1) {
isMagic = true;
break;
}
}
if (isMagic) {
System.out.println(word + " is a Magic String.");
} else {
System.out.println(word + " is NOT a Magic String.");
}
sc.close();
}
}
Verified Console / Dry-Run Output:
Enter a word: Cabinet
CABINET is a Magic String.
5. CISCE Variable Description Table (VDT) & Dry Run
| Variable | Data Type | Exam Purpose & Sample Value |
|---|---|---|
| word | String | Uppercase input word to test, e.g. "CABINET" |
| len | int | Length of word (7), setting loop boundary len - 1 |
| isMagic | boolean | Flag initialized to false; turns true when ch2 - ch1 == 1 |
| i | int | Loop index from 0 to len - 2, stopping at 1 via break for "CABINET" |
| ch1 | char | Left character word.charAt(i), e.g. 'A' (65) at i = 1 |
| ch2 | char | Right character word.charAt(i + 1), e.g. 'B' (66) at i + 1 = 2 |
Conquer ASCII & Flag-Based String Programs in 1-on-1 Classes
Build rock-solid intuition for char arithmetic, boolean flags, and edge-case dry runs with guided BlueJ practice designed for ICSE Class 9 and 10.
6. Common Mistakes & Top Scorer Tips
- Avoid Mistake: Writing Math.abs(ch2 - ch1) == 1 when the exam only accepts forward alphabetical order (AB, not BA).
- Avoid Mistake: Printing "Magic String" inside the else branch of the loop on the first non-consecutive pair before later pairs like AB are checked.
- Avoid Mistake: Omitting Character.isLetter(), which wrongly flags digit strings like "A12B" as Magic Strings because '2' - '1' == 1.
- Examiner / Mentor Tip: Memorize core ASCII anchor codes for Section A: 'A' = 65, 'Z' = 90, 'a' = 97, 'z' = 122, and '0' = 48.
- Examiner / Mentor Tip: Mention why break; is used—once isMagic becomes true, scanning remaining characters cannot change the outcome.
- Examiner / Mentor Tip: Check single-letter input edge cases ("A"): len - 1 is 0, so the loop is skipped and isMagic stays false.
7. Interactive Concept Check Quiz
Question: Given word = "THYME" (already uppercase), which adjacent pair makes MagicStringCheck print "THYME is a Magic String."?
8. Frequently Asked Questions
What is the ASCII value difference for consecutive letters in Java?
Should "BA" or "DC" count as a Magic String in ICSE exams?
What happens if a sentence with spaces is entered instead of a single word?
Explore Related Lessons & Free Interactive Tools
- All 110 Guides in ICSE Java BlueJ Library
- Happy Word Program in Java Using Alphabet Position Values
- Caesar Cipher and ROT13 Letter Shift Encryption in Java
- Capitalize the First Letter of Each Word (Title Case) in Java
- Interactive Tool: ICSE Class 10 Java BlueJ Output Quiz
- Interactive Tool: ICSE Variable Description Table (VDT) Auto-Formatter
