Mirrored Alphabet, Spaces in Java

Beginner
⏱️ 7 min read
📚 Updated: Aug 2025
🎯 2 Code Examples
Nested Loops

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):

Output
A        A
AB      BA
ABC    CBA
ABCD  DCBA
ABCDEEDCBA
1

Complete Java Program (A–E)

Two full-width passes. Left prints letters when j <= i; right prints spaces while k > i, then letters.

Java
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

1

Outer i

Current peak letter for the row (A through E).

Rows
2

Left ramp

j spans A..E. Print the letter only while j <= i, otherwise print a space.

j <= i
3

Right ramp

k spans E..A. Print spaces while k > i, then print letters when k <= i.

k > i
4

Gap shrinks

The space band is 2 * (endChar - i): each side contributes endChar - i padding slots.

Formula
=

Work per row

Two passes of length n for n letters → O(n²) total.

2

Variation — User Input

startCharendChar replace 'A' and 'E' in both loops. Clamp rows to 1–26.

Java
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 <= 26 for a single alphabet span

Avoid

  • Mixing up k >= i vs k > i (duplicates or drops the peak on the right)
  • Using different endChar in the two inner loops (breaks alignment)

Key Takeaways

1

Symmetric gutters come from complementary conditions on two full-width passes.

2

j <= i builds the left ramp; k > i pads before the right ramp.

3

Last row: no spaces if i == endChar.

4

O(n²) for n letters in the range.

❓ Frequently Asked Questions

Eight: four from the tail of the first loop (positions after A) and four from the start of the second (before the right A).
When i == endChar, no j or k triggers the space branches, so letters are contiguous.
Program 18 prints one palindrome per row. Here each row is two mirrored halves with a variable gap; they match only when the gap goes to zero on the last row.
O(n²) for n rows over an n-letter span.

More Java alphabet patterns

Two mirrored halves with a spacer column is the same idea as butterfly or hourglass star programs.

All Alphabet Patterns →
Did you know?

The number of space characters on a row (before the last) is 2 * (endChar - i): each side contributes endChar - i padding slots.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

12 people found this page helpful