Shape Rule
Mirror + gap
Left ramp, spaces, right ramp — gap shrinks each row.

Build rows that widen toward the middle: letters on the left, a shrinking space band, then the mirrored letters on the right — until the last row meets as ABCDEEDCBA. Compare Program 18 (palindrome, no gap) and Program 15 (stars in the middle). Includes a live preview, worked Java examples, edge cases, and complexity.
Mirror + gap
Left ramp, spaces, right ramp — gap shrinks each row.
Row peak
i grows from 0..n so more letters fill each half.
j <= i
Print letters on the left; fill the rest with spaces.
k > i
Spaces first, then mirrored letters down to A.
Top letter
Pick A–J and draw the mirrored gap pattern instantly.
Complexity
Each of n rows scans n columns twice.
A mirrored alphabet pattern with spaces keeps a fixed total width and splits each row into two scans: grow letters on the left, then print a shrinking gap and the mirror on the right.
In Java you solve it with nested loops and simple conditions — j <= i on the left and k > i on the right decide letter vs space.
It teaches fixed-width dual passes — the same idea behind many butterfly and mirrored-gap patterns, with spaces instead of stars.
Both halves scan the same n columns.
Letters when j <= i.
Letters when k <= i.
Spaces vanish on the last row.
In short: for each row peak i, scan left 0..n (letter or space), then right n..0 (space or letter), then call println().
Given a top letter (like E), print n+1 rows of mirrored alphabet halves with a shrinking middle gap.
// Classic sample (A–E; spaces shown as gaps)
// A A
// AB BA
// ABC CBA
// ABCD DCBA
// ABCDEEDCBA | Item | Type | Description |
|---|---|---|
top / n | char / int | Last letter (e.g. E) or last index n = top − ‘A’. |
| Printed output | text | Mirrored ramps with spaces; final row has no gap. |
for i from 0 to n:
for j from 0 to n:
print alpha[j] if j <= i else space
for k from n down to 0:
print space if k > i else alpha[k]
print newline | Approach | Idea | Best for |
|---|---|---|
| Two fixed-width scans | Letter-or-space in each cell | Matching this classic sample |
| Letters + gap + mirror | Print left letters, then gap count, then reverse | Clearer reading / teaching rewrite |
| Goal | Pattern |
|---|---|
| Rows | for (int i = 0; i <= n; i++) |
| Left half | System.out.print(j <= i ? alpha[j] : ' '); |
| Right half | System.out.print(k > i ? ' ' : alpha[k]); |
| Shared width | Both loops scan 0..n (or n..0) |
| End the row | System.out.println(); |
| No gap (palindrome) | See Program 18 |
Same row — different roles on each pass.
j <= iGrowing ramp A..peak on the left
gapFill remaining left columns + early right columns
k <= iMirrored ramp peak..A on the right
breakEnds the row after both passes
Reach for this when teaching fixed-width mirrors and shrinking gaps.
Same mirror idea, but keep a visible middle gap until the end.
Practice left/right ramps with a shared width.
Swap middle spaces for * and compare with Program 15.
See why both halves must share the same column count.
This is a console teaching pattern — not how you build modern app screens.
Key benefit: two simple conditions turn a flat alphabet scan into a shrinking mirrored gap.
Enter a top letter from A to J and draw the mirrored alphabet-with-spaces pattern in the browser.
Three complete Java programs — fixed A–E, user-chosen top letter, and an explicit letters-gap-mirror rewrite. Click View Output to reveal sample console results.
Print five mirrored rows with two fixed-width scans.
Two fixed-width scans per row. Conditions decide whether to print a letter or a space.
public class MirroredSpaces {
public static void main(String[] args) {
char[] alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray();
for (int i = 0; i <= 4; i++) {
for (int j = 0; j <= 4; j++) {
if (j <= i)
System.out.print(alpha[j]);
else
System.out.print(" ");
}
for (int k = 4; k >= 0; k--) {
if (k > i)
System.out.print(" ");
else
System.out.print(alpha[k]);
}
System.out.println();
}
}
} When i = 2, the left pass prints ABC then two spaces; the right pass prints two spaces then CBA → ABC CBA. When i = 4, every column is a letter on both sides → ABCDEEDCBA.
Let the user choose the last letter.
Build the full width dynamically from the chosen top letter. Prefer validating a single A–Z character from next()/charAt(0) in real apps.
import java.util.Scanner;
public class MirroredSpacesInput {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the top letter (like E): ");
char top = sc.next().toUpperCase().charAt(0);
int n = top - 'A';
char[] alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray();
for (int i = 0; i <= n; i++) {
for (int j = 0; j <= n; j++)
System.out.print(j <= i ? alpha[j] : ' ');
for (int k = n; k >= 0; k--)
System.out.print(k > i ? ' ' : alpha[k]);
System.out.println();
}
sc.close();
}
} n = top - 'A' sets the shared half-width. With top = 'C', the last row meets as ABCCBA with no gap.
Same shape with letters, then an explicit gap, then the mirror.
Often clearer to read: print left letters, print 2*(n-i) spaces, then print the reverse letters.
public class MirroredSpacesExplicit {
public static void main(String[] args) {
int n = 4; // last index (E)
for (int i = 0; i <= n; i++) {
for (int j = 0; j <= i; j++)
System.out.print((char)('A' + j));
for (int s = 0; s < 2 * (n - i); s++)
System.out.print(' ');
for (int k = i; k >= 0; k--)
System.out.print((char)('A' + k));
System.out.println();
}
}
} Gap size is 2*(n - i) — the leftover columns that the classic dual scan would fill with spaces on both halves. On the last row the gap is 0, so the halves meet (and the peak letter appears twice: once from each half).
Both halves scan a fixed range (0..n), so each row has a consistent total width.
For column j, print alpha[j] if j <= i, else print a space.
Scan from the end: while k > i print spaces; once k <= i, print alpha[k].
System.out.println() ends the row so the next peak starts fresh.
n letters ⇒ n+1 rows × 2n columns — O(n²) time, O(1) extra memory.
Trace each row peak and how many spaces sit between the halves.
i | Left | Gap spaces | Right | Printed row |
|---|---|---|---|---|
0 | A + 4 spaces | 8 total across halves | 4 spaces + A | A········A |
1 | AB + 3 spaces | 6 | 3 spaces + BA | AB······BA |
2 | ABC + 2 spaces | 4 | 2 spaces + CBA | ABC····CBA |
3 | ABCD + 1 space | 2 | 1 space + DCBA | ABCD··DCBA |
4 | ABCDE | 0 | EDCBA | ABCDEEDCBA |
Gap spaces per row follow 2*(n - i) with n = 4.
Where this mirrored-gap idea shows up beyond the homework prompt.
Clearest alphabet demo of two fixed-width scans per row.
Example: print left only, then add the right pass.
Same mirror letters — with or without a middle gap.
Example: side-by-side gap vs continuous palindrome.
Replace middle spaces with * (see Program 15).
Example: print . while debugging gap size.
Teach 2*(n-i) as an alternative to dual scans.
Example: compare Examples 1 and 3 outputs.
Two n-wide passes make O(n²) easy to count.
Example: 5 rows × 10 cells = 50 writes.
Practice reading and validating a single top letter.
Example: reject empty strings and non A–Z input.
Pro Tip: say “left ramp, spaces, right mirror” before coding — that story prevents mismatched half widths.
Why this pattern earns a spot after continuous palindrome pyramids.
Wrong bounds or unequal halves show up as a broken mirror immediately.
Dual scans or explicit gap counts teach the same shape.
Spaces, dots, or stars in the gap are one-character changes.
Streaming output needs no storage beyond loop variables.
Pro Tip: learn the classic dual-scan version first; treat the explicit gap rewrite as a clarity upgrade afterward.
Small habits that keep mirrored-gap code clean.
Left and right halves must scan the same n or the mirror breaks.
Tabs change width by editor settings and ruin alignment.
Require a single A–Z character; empty input breaks charAt(0).
Temporarily print . instead of spaces to count the gap.
Trace ABC····CBA on paper before coding larger tops.
Pro Tip: if the last row still has a gap, your peak never reaches the final index n.
Mistakes that commonly break mirrored alphabet-with-spaces patterns.
Different bounds for left and right break the mirror alignment.
→ Both passes must share the same n.
Using j > i for letters on the left prints spaces first.
→ Left: letter when j <= i; right: space when k > i.
Alignment depends on the editor’s tab size.
→ Always print a single space character.
charAt(0)Empty or multi-character input can throw or pick the wrong char.
→ Read a string, check length, take [0], validate A–Z.
Breaks the row into one character per line.
→ Call println only after both halves finish.
Check these inputs before calling the solution done.
Output is AA (no gap).
Five rows ending in ABCDEEDCBA.
Last row is ABCCBA (Example 2).
Reject or cap so indices stay in A–Z.
charAt(0) fails on empty tokens — validate first.
Same loops; only the fill character changes.
Try these variations to lock in the pattern.
*2*(n-i) spaces (Example 3)0..n.2*(n - i); zero on the final row.Quick Takeaway: scan left (letter or space), scan right (space or letter), shrink the gap each row until the halves meet.
| Program | Time | Extra space |
|---|---|---|
| Dual fixed-width scans (Examples 1–2) | O(n²) | O(1) |
| Explicit gap (Example 3) | O(n²) | O(1) |
With last index n, each of the n+1 rows prints 2(n+1) cells, so total work is O(n²).
The mirrored alphabet-with-spaces pattern is a small nested-loop exercise with lasting payoff: fixed-width dual passes, letter-vs-space conditions, and a gap that shrinks to zero. Master the classic A–E sample, then try user input and the explicit gap rewrite.
Practice the three examples above, then continue to Program 20’s right-aligned reverse alphabet pyramid.
Share one width for both halves, print letters when inside the peak, fill the rest with spaces, and break only after both passes.
i == n so the gap closesj <= i / k > i conditionsprintln inside a half loopcharAt(0)Print the mirrored alphabet-with-spaces pattern the beginner-friendly way.
Mirror + shrinking gap
Definitionj <= i → letter
Codek > i → space
CodeEnds each row
I/OO(n²) time
AnalysisEach row uses two fixed-width scans from A to E. The first builds the left ramp (letters when j <= i else spaces). The second builds the right ramp (spaces while k > i, else letters). The gap shrinks until the last row meets as ABCDEEDCBA.
Next up: right-aligned reverse alphabet pyramids (····A, ···BA, …, EDCBA).
12 people found this page helpful