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 C 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 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.
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 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").
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 j if j <= i else space
for k from n down to 0:
print space if k > i else 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 (i = 'A'; i <= top; ++i) |
| Left half | if (j <= i) printf("%c", j); else printf(" "); |
| Right half | if (k > i) printf(" "); else printf("%c", k); |
| Shared width | Both loops scan 'A'..top (or top..'A') |
| End the row | printf("\n"); |
| 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 C programs — fixed A–E, scanf top letter, and an explicit gap 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.
#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;
} When i = 'C', the left pass prints ABC then two spaces; the right pass prints two spaces then CBA → ABC CBA. When i = 'E', 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. Check scanf(" %c", &top) and validate A–Z in real apps.
#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;
} Both loops scan 'A'..top. 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.
#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;
} 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 ('A'..top), so each row has a consistent total width.
For column j, print j if j <= i, else print a space.
Scan from the end: while k > i print spaces; once k <= i, print k.
printf("\n") 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 scanf breaks scanf.
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.
scanfEmpty or multi-character input leaves top wrong or uninitialized.
→ Check scanf’s return value; use scanf(" %c", &top) and validate A–Z.
printf("\n") Mid-PassBreaks the row into one character per line.
→ Call printf("\n") 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.
scanf can throw — 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 conditionsprintf("\n") inside a half loopscanfPrint 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.
Keep exploring alphabet patterns with nested loops.
12 people found this page helpful