Mirrored Alphabet, Spaces in C

Beginner
⏱️ 8 min read
📚 Updated: Aug 2026
🎯 3 Code Examples
🚀 Live Preview
Nested Loops

What You’ll Learn

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 C examples, edge cases, and complexity.

Shape Rule

Mirror + gap

Left ramp, spaces, right ramp — gap shrinks each row.

Outer Loop

Row peak

i grows from 0..n so more letters fill each half.

Left Pass

j <= i

Print letters on the left; fill the rest with spaces.

Right Pass

k > i

Spaces first, then mirrored letters down to A.

Live Preview

Top letter

Pick A–J and draw the mirrored gap pattern instantly.

O(n²)

Complexity

Each of n rows scans n columns twice.

Introduction

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 C you solve it with nested loops and simple conditions — j <= i on the left and k > i on the right decide letter vs space.

Why it matters?

It teaches fixed-width dual passes — the same idea behind many butterfly and mirrored-gap patterns, with spaces instead of stars.

Key Highlights

Fixed Width

Both halves scan the same n columns.

Left Ramp

Letters when j <= i.

Right Mirror

Letters when k <= i.

Shrinking Gap

Spaces vanish on the last row.

In short: for each peak i, scan left A..top (letter if j <= i else space), scan right top..A (space if k > i else letter), then printf("\n").

📝 Problem & Approach

Given a top letter (like E), print n+1 rows of mirrored alphabet halves with a shrinking middle gap.

c
// Classic sample (A–E; spaces shown as gaps)
// A        A
// AB      BA
// ABC    CBA
// ABCD  DCBA
// ABCDEEDCBA

Inputs & Outputs

ItemTypeDescription
top / nchar / intLast letter (e.g. E) or last index n = top − ‘A’.
Printed outputtextMirrored ramps with spaces; final row has no gap.

Minimal workflow

Pseudocode
for i from 0 to n:
    for j from 0 to n:
        print j if j <= i else space
    for k from n down to 0:
        print space if k > i else k
    print newline

Approach comparison

ApproachIdeaBest for
Two fixed-width scansLetter-or-space in each cellMatching this classic sample
Letters + gap + mirrorPrint left letters, then gap count, then reverseClearer reading / teaching rewrite

⚡ Quick Reference

GoalPattern
Rowsfor (i = 'A'; i <= top; ++i)
Left halfif (j <= i) printf("%c", j); else printf(" ");
Right halfif (k > i) printf(" "); else printf("%c", k);
Shared widthBoth loops scan 'A'..top (or top..'A')
End the rowprintf("\n");
No gap (palindrome)See Program 18

📋 Letter vs Space vs printf

Same row — different roles on each pass.

Left letter
j <= i

Growing ramp A..peak on the left

Middle spaces
gap

Fill remaining left columns + early right columns

Right letter
k <= i

Mirrored ramp peak..A on the right

printf("\n")
break

Ends the row after both passes

Context

When This Pattern Shows Up

Reach for this when teaching fixed-width mirrors and shrinking gaps.

  1. After Program 18

    Same mirror idea, but keep a visible middle gap until the end.

  2. Butterfly warm-ups

    Practice left/right ramps with a shared width.

  3. Space vs star labs

    Swap middle spaces for * and compare with Program 15.

  4. Alignment drills

    See why both halves must share the same column count.

  5. Not a UI layout tool

    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.

🔮 Live Preview

Enter a top letter from A to J and draw the mirrored alphabet-with-spaces pattern in the browser.

Try E (classic sample) or C (smaller). Use a single letter A–J for a readable preview.

Live result
Press "Draw pattern".

Examples Gallery

Three complete C programs — fixed A–E, scanf top letter, and an explicit gap rewrite. Click View Output to reveal sample console results.

📚 Getting Started

Print five mirrored rows with two fixed-width scans.

Example 1 — Fixed A–E

Two fixed-width scans per row. Conditions decide whether to print a letter or a space.

c
#include <stdio.h>

int main() {
    int i, j, k;

    for (i = 'A'; i <= 'E'; ++i) {
        for (j = 'A'; j <= 'E'; ++j) {
            if (j <= i) {
                printf("%c", j);
            } else {
                printf(" ");
            }
        }
        for (k = 'E'; k >= 'A'; --k) {
            if (k > i) {
                printf(" ");
            } else {
                printf("%c", k);
            }
        }
        printf("\n");
    }

    return 0;
}

How It Works

When i = 'C', the left pass prints ABC then two spaces; the right pass prints two spaces then CBAABC CBA. When i = 'E', every column is a letter on both sides → ABCDEEDCBA.

📈 Practical Variant

Let the user choose the last letter.

Example 2 — Top Letter Input

Build the full width dynamically from the chosen top letter. Check scanf(" %c", &top) and validate A–Z in real apps.

c
#include <stdio.h>

int main() {
    int i, j, k;
    char top;

    printf("Enter the top letter (like E): ");
    scanf(" %c", &top);

    for (i = 'A'; i <= top; ++i) {
        for (j = 'A'; j <= top; ++j) {
            if (j <= i) {
                printf("%c", j);
            } else {
                printf(" ");
            }
        }
        for (k = top; k >= 'A'; --k) {
            if (k > i) {
                printf(" ");
            } else {
                printf("%c", k);
            }
        }
        printf("\n");
    }

    return 0;
}

How It Works

Both loops scan 'A'..top. With top = 'C', the last row meets as ABCCBA with no gap.

⚡ Explicit Style

Same shape with letters, then an explicit gap, then the mirror.

Example 3 — Letters, Gap Count, Mirror

Often clearer to read: print left letters, print 2*(n-i) spaces, then print the reverse letters.

c
#include <stdio.h>

int main() {
    int n = 4; /* last index (E) */
    int i, j, s, k;

    for (i = 0; i <= n; ++i) {
        for (j = 0; j <= i; ++j) {
            printf("%c", 'A' + j);
        }

        for (s = 0; s < 2 * (n - i); ++s) {
            printf(" ");
        }

        for (k = i; k >= 0; --k) {
            printf("%c", 'A' + k);
        }

        printf("\n");
    }

    return 0;
}

How It Works

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

🧠 How the Algorithm Prints Rows

1

Fix the width

Both halves scan a fixed range ('A'..top), so each row has a consistent total width.

Width
2

Paint the left ramp

For column j, print j if j <= i, else print a space.

Left
3

Paint the right ramp

Scan from the end: while k > i print spaces; once k <= i, print k.

Right
4

New line

printf("\n") ends the row so the next peak starts fresh.

Break
=

Gap shrinks each row

n letters ⇒ n+1 rows × 2n columns — O(n²) time, O(1) extra memory.

🔎 Worked Walkthrough — A–E

Trace each row peak and how many spaces sit between the halves.

iLeftGap spacesRightPrinted row
0A + 4 spaces8 total across halves4 spaces + AA········A
1AB + 3 spaces63 spaces + BAAB······BA
2ABC + 2 spaces42 spaces + CBAABC····CBA
3ABCD + 1 space21 space + DCBAABCD··DCBA
4ABCDE0EDCBAABCDEEDCBA

Gap spaces per row follow 2*(n - i) with n = 4.

Use Cases

Where this mirrored-gap idea shows up beyond the homework prompt.

1. Dual-Pass Practice

Clearest alphabet demo of two fixed-width scans per row.

Example: print left only, then add the right pass.

2. Pair with Program 18

Same mirror letters — with or without a middle gap.

Example: side-by-side gap vs continuous palindrome.

3. Star-Fill Labs

Replace middle spaces with * (see Program 15).

Example: print . while debugging gap size.

4. Explicit Gap Rewrite

Teach 2*(n-i) as an alternative to dual scans.

Example: compare Examples 1 and 3 outputs.

5. Complexity Intuition

Two n-wide passes make O(n²) easy to count.

Example: 5 rows × 10 cells = 50 writes.

6. Char Validation

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.

Advantages

Why this pattern earns a spot after continuous palindrome pyramids.

  1. 1. Instant Visual Feedback

    Wrong bounds or unequal halves show up as a broken mirror immediately.

  2. 2. Two Clear Rewrites

    Dual scans or explicit gap counts teach the same shape.

  3. 3. Easy Marker Swaps

    Spaces, dots, or stars in the gap are one-character changes.

  4. 4. O(1) Extra Memory

    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.

Usage Tips

Small habits that keep mirrored-gap code clean.

  1. 1. Keep Matching Widths

    Left and right halves must scan the same n or the mirror breaks.

  2. 2. Use Spaces, Not Tabs

    Tabs change width by editor settings and ruin alignment.

  3. 3. Validate Letter Input

    Require a single A–Z character; empty scanf breaks scanf.

  4. 4. Debug with Dots

    Temporarily print . instead of spaces to count the gap.

  5. 5. Dry-Run Row C

    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.

Common Pitfalls

Mistakes that commonly break mirrored alphabet-with-spaces patterns.

  1. 1. Unequal Half Widths

    Different bounds for left and right break the mirror alignment.

    → Both passes must share the same n.

  2. 2. Swapping the Conditions

    Using j > i for letters on the left prints spaces first.

    → Left: letter when j <= i; right: space when k > i.

  3. 3. Tabs Instead of Spaces

    Alignment depends on the editor’s tab size.

    → Always print a single space character.

  4. 4. Unchecked scanf

    Empty or multi-character input leaves top wrong or uninitialized.

    → Check scanf’s return value; use scanf(" %c", &top) and validate A–Z.

  5. 5. printf("\n") Mid-Pass

    Breaks the row into one character per line.

    → Call printf("\n") only after both halves finish.

Edge Cases

Check these inputs before calling the solution done.

top = A

Single row

Output is AA (no gap).

top = E

Classic sample

Five rows ending in ABCDEEDCBA.

top = C

Smaller grid

Last row is ABCCBA (Example 2).

Past Z

Invalid top

Reject or cap so indices stay in A–Z.

Bad input

Empty scanf

scanf can throw — validate first.

Gap mark

. or *

Same loops; only the fill character changes.

🎯 Practice Problems

Try these variations to lock in the pattern.

1. Remove the gap

  • Print continuous palindromes
  • Compare with Program 18

2. Fill with stars

3. Explicit gap version

  • Use 2*(n-i) spaces (Example 3)
  • Confirm output matches dual scans

4. Continue to Program 20

Notes

  • Shared width. Left and right halves both scan 0..n.
  • Gap size is 2*(n - i); zero on the final row.
  • On the last row the peak letter appears twice (once per half).
  • Prefer spaces over tabs for stable monospace alignment.

Quick Takeaway: scan left (letter or space), scan right (space or letter), shrink the gap each row until the halves meet.

⏱️ Time and Space Complexity

ProgramTimeExtra 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²).

Wrap Up

🎉 Conclusion

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.

💡 Best Practices

✅ Do

  • Use the same width for left and right halves
  • Print spaces (not tabs) in the gap
  • Let the last row reach i == n so the gap closes
  • Validate a single A–Z character on input
  • State O(n²) when asked about complexity

❌ Don’t

  • Mismatched bounds between halves
  • Swap j <= i / k > i conditions
  • Call printf("\n") inside a half loop
  • Assume empty input is safe for scanf
  • Use tabs for alignment in console patterns

Key Takeaways

Knowledge Unlocked

Five things to remember about this alphabet pattern

Print the mirrored alphabet-with-spaces pattern the beginner-friendly way.

5
Core concepts
L 02

Left

j <= i → letter

Code
R 03

Right

k > i → space

Code
04

New line

Ends each row

I/O
O 05

Complexity

O(n²) time

Analysis

❓ Frequently Asked Questions

The left loop builds the increasing part and fills the remaining columns with spaces. The right loop fills spaces until the peak, then prints the decreasing mirror.
The spaces keep both halves fixed width so the mirror effect is aligned. The gap shrinks each row until both halves touch.
When i reaches the last letter (E), all positions satisfy the letter conditions on both sides, so both halves print letters and meet as ABCDEEDCBA.
Increase the last index/letter and update the loop bounds so the left and right halves each scan the new width.
The first forms print a cell and stay on the same line. printf("\n") ends the row after both halves finish.
Program 18 prints a continuous palindrome with no middle gap. This pattern keeps a shrinking space band between left and right ramps until the final row.
O(n²) for n letters because each row scans n columns twice (left + right).
For a top letter, use scanf(" %c", &top) and require A–Z. Or read rows with scanf("%d", &rows) and set endChar = 'A' + rows - 1.

Did you Know? 🔊

Each 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.

Continue to Alphabet Pattern 20

Keep exploring alphabet patterns with nested loops.

Program 20 tutorial →

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