Mirrored Alphabet, Spaces in Java

What You’ll Learn
Build rows that widen toward the middle: letters on the left, spaces, then the mirror on the right. The bottom row touches with no space band.
Compare program 18 (palindrome without a gap) and program 15 (stars instead of spaces in the gap).
⭐ Pattern Output
Five rows from A to E (spaces shown as gaps in monospace):
A A
AB BA
ABC CBA
ABCD DCBA
ABCDEEDCBAComplete Java Program (A–E)
Two full-width passes. Left prints letters when j <= i; right prints spaces while k > i, then letters.
public class Main {
public static void main(String[] args) {
int i, j, k;
for (i = 65; i <= 69; i++) {
for (j = 65; j <= 69; j++) {
if (j <= i) {
System.out.print((char) j);
} else {
System.out.print(" ");
}
}
for (k = 69; k >= 65; k--) {
if (k > i) {
System.out.print(" ");
} else {
System.out.print((char) k);
}
}
System.out.println();
}
}
}🧠 How It Works
Outer i
Current peak letter for the row (A through E).
Left ramp
j spans A..E. Print the letter only while j <= i, otherwise print a space.
Right ramp
k spans E..A. Print spaces while k > i, then print letters when k <= i.
Gap shrinks
The space band is 2 * (endChar - i): each side contributes endChar - i padding slots.
Work per row
Two passes of length n for n letters → O(n²) total.
Variation — User Input
startChar … endChar replace 'A' and 'E' in both loops. Clamp rows to 1–26.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number of rows: ");
int rows = sc.nextInt();
rows = Math.max(1, Math.min(rows, 26));
char startChar = 'A';
char endChar = (char) ('A' + rows - 1);
for (char i = startChar; i <= endChar; i++) {
for (char j = startChar; j <= endChar; j++) {
if (j <= i) {
System.out.print(j);
} else {
System.out.print(" ");
}
}
for (char k = endChar; k >= startChar; k--) {
if (k > i) {
System.out.print(" ");
} else {
System.out.print(k);
}
}
System.out.println();
}
sc.close();
}
}💡 Tips for Enhancement
Try These
- Swap spaces for
.or-to see the gutter clearly - Refactor: compute space count
2 * (endChar - i)in one loop - Keep
rows <= 26for a single alphabet span
Avoid
- Mixing up
k >= ivsk > i(duplicates or drops the peak on the right) - Using different
endCharin the two inner loops (breaks alignment)
Key Takeaways
Symmetric gutters come from complementary conditions on two full-width passes.
j <= i builds the left ramp; k > i pads before the right ramp.
Last row: no spaces if i == endChar.
O(n²) for n letters in the range.
❓ Frequently Asked Questions
A) and four from the start of the second (before the right A).i == endChar, no j or k triggers the space branches, so letters are contiguous.More Java alphabet patterns
Two mirrored halves with a spacer column is the same idea as butterfly or hourglass star programs.
The number of space characters on a row (before the last) is 2 * (endChar - i): each side contributes endChar - i padding slots.
12 people found this page helpful
