Three Stages
Pad + up + mirror
Spaces, ascending A..i, descending (i-1)..A.

Print centered palindromic rows — A, ABA, ABCBA, up to ABCDEDCBA — by padding with spaces, climbing A..i, then mirroring (i-1)..A without repeating the middle letter. Compare Program 18 and Program 24 for related palindrome pyramids. Includes a live preview, worked PHP examples, edge cases, and complexity.
Pad + up + mirror
Spaces, ascending A..i, descending (i-1)..A.
Leading spaces
Pad so each row sits under the widest base.
$n = $k - 1
Then --$n mirrors without a double peak.
2i+1 letters
Each row climbs to i and mirrors back to A.
Top letter
Pick a top letter (A–F) and draw the pyramid.
Complexity
n rows × O(n) spaces and letters each.
A symmetric alphabet pyramid centers growing palindromes so the peak letter of each row sits in the middle and the sides mirror around it.
In PHP you pad with spaces, print A..i, then use a bridge index so the descending half starts at i-1 and never repeats the center.
It combines centering, ascending char loops, and careful mirroring — a core trio for many pyramid and diamond alphabet labs.
Leading spaces center rows.
Print A through i.
Print (i-1) down to A.
Bridge n skips a double center.
In short: for each row $i, print $end-$i spaces, print A..$i, set $n = $k-1, print --$n for $i steps, then echo PHP_EOL.
Given a top letter (or fixed E), print a centered pyramid of alphabet palindromes ending at that letter.
// Five rows (top = E)
// A
// ABA
// ABCBA
// ABCDCBA
// ABCDEDCBA | Item | Type | Description |
|---|---|---|
top / end | string / int | Top letter; $end = ord($top) - ord('A') (4 for E). Rows = end+1. |
| Printed output | text | Centered palindrome rows from A up to the top letter. |
$end = ord($top) - ord('A')
for i from 0 to end:
print (end - i) spaces
for k from 0 to i: // ascending A..i
print letter[k]
$n = $k - 1 // peak index
repeat i times:
print letter[--$n] // (i-1)..A
print newline | Approach | Idea | Best for |
|---|---|---|
| Bridge variable | $n = $k-1 then --$n for the mirror | Matching this classic sample |
| Explicit second loop | for ($p = $i-1; $p >= 0; $p--) echo $alpha[$p] | When you want clearer bounds |
| Goal | Pattern |
|---|---|
| Alphabet + end | $alpha = str_split("ABCDEFG..."); $end = 4; |
| Rows | for ($i = 0; $i <= $end; $i++) |
| Leading spaces | for ($j = $end; $j > $i; $j--) echo " "; |
| Ascending | for ($k = 0; $k <= $i; $k++) echo $alpha[$k]; |
| Mirror | $n = $k - 1; for ($m = 0; $m < $i; $m++) echo $alpha[--$n]; |
| Inverted V next | See Program 33 |
Four roles that build each centered palindrome row.
padCenters the row under the base
upAscending half including the peak
mirrorDescending half without the peak
breakEnds the row after all three stages
Reach for this when teaching centered palindromes with a carefully skipped peak letter.
Switch from sparse diagonals to filled centered pyramids.
Practice ascending then mirroring without a double center.
Learn the classic $n = $k - 1 / --$n idiom.
Next opens an inverted V with diagonal letters.
This is a console teaching pattern — not how you build modern app screens.
Key benefit: pad, ascend to the peak, then mirror with --$n is the cleanest classic way to print centered alphabet palindromes.
Choose a top letter from A to F and draw the symmetric alphabet pyramid in the browser.
Three complete PHP programs — fixed A–E, user-chosen top letter, and an explicit-mirror rewrite. Click View Output to reveal sample console results.
Print five centered palindrome rows from A to ABCDEDCBA.
Matches the reference logic using a bridge variable $n = $k - 1 to print the descending half.
<?php
$alpha = str_split("ABCDEFGHIJKLMNOPQRSTUVWXYZ");
for ($i = 0; $i <= 4; $i++) {
for ($j = 4; $j > $i; $j--)
echo " ";
for ($k = 0; $k <= $i; $k++)
echo $alpha[$k];
$n = $k - 1;
for ($m = 0; $m < $i; $m++)
echo $alpha[--$n];
echo PHP_EOL;
} When i = 2, spaces pad twice, ascending prints ABC, then --$n prints B A → ABCBA. The peak C is printed only once.
Let the user pick the top letter (like E).
The loops adapt to the new size automatically. Prefer validating a single A–Z character from the CLI input string in real apps.
<?php
echo "Enter top letter (like E): ";
$top = strtoupper(trim(fgets(STDIN)))[0];
$end = ord($top) - ord('A');
$alpha = str_split("ABCDEFGHIJKLMNOPQRSTUVWXYZ");
for ($i = 0; $i <= $end; $i++) {
for ($j = $end; $j > $i; $j--)
echo " ";
for ($k = 0; $k <= $i; $k++)
echo $alpha[$k];
$n = $k - 1;
for ($m = 0; $m < $i; $m++)
echo $alpha[--$n];
echo PHP_EOL;
} $end = ord($top) - ord('A') scales padding, ascending, and mirror loops together. For top = C you get three centered rows.
Same pyramid with an explicit descending loop instead of --$n.
Often easier to read: after printing A..$i, loop $p from $i-1 down to 0.
<?php
$end = 4;
$alpha = str_split("ABCDEFGHIJKLMNOPQRSTUVWXYZ");
for ($i = 0; $i <= $end; $i++) {
for ($j = $end; $j > $i; $j--)
echo " ";
for ($k = 0; $k <= $i; $k++)
echo $alpha[$k];
for ($p = $i - 1; $p >= 0; $p--)
echo $alpha[$p];
echo PHP_EOL;
} The explicit $p = $i - 1 .. 0 loop makes the skipped peak obvious. Output matches the bridge-variable version exactly.
Loop j from end down while j > i to center the pyramid under the widest row.
Loop k = 0..i and print alpha[k] so the row climbs from A to the peak letter.
Set $n = $k - 1, then print --$n for i steps so the tail is (i-1)..A.
Ascending prints i+1 letters; descending prints i letters. Together with spaces, the pyramid stays centered.
Each row climbs from A to the row letter, then mirrors back to A — O(n²) time.
Trace padding, ascending half, and mirrored descending half for each row.
i | Spaces | Ascending | Mirror | Printed row |
|---|---|---|---|---|
0 | 4 | A | (none) | A |
1 | 3 | AB | A | ABA |
2 | 2 | ABC | BA | ABCBA |
3 | 1 | ABCD | CBA | ABCDCBA |
4 | 0 | ABCDE | DCBA | ABCDEDCBA |
After ascending, k = i + 1, so $n = $k - 1 = i. The first --$n lands on i - 1.
Where this symmetric alphabet pyramid shows up beyond the homework prompt.
Clearest demo of climb-then-mirror alphabet rows.
Example: start the mirror at i and watch a double peak.
Reuse leading-space padding for other pyramids.
Example: change j > i to j >= i and see a shift.
Practice $n = $k - 1 then --$n.
Example: rewrite with explicit p = i-1..0 (Example 3).
Scale with $end = ord($top) - ord('A').
Example: grow from E to H without rewriting loops.
Growing palindromes make O(n²) easy to see.
Example: row lengths 1+3+5+7+9 = 25 letters.
Next draws an inverted V with diagonal letters.
Example: continue to Program 33.
Pro Tip: say “pad, A..i, then (i-1)..A” before coding — that story prevents a double peak like ABCCBA.
Why this pattern earns a spot after V-shaped diagonals.
Bad padding or a double peak shows up immediately.
Pad, ascend, and mirror are easy to explain separately.
Change end and the whole pyramid grows.
Bridge --$n or explicit p = i-1..0 both work.
Pro Tip: learn the reference bridge style first if you are matching exam code; switch to the explicit mirror when readability matters more.
Small habits that keep symmetric alphabet pyramids clean.
Starting at i duplicates the peak letter.
Using j >= i adds an extra leading space.
Use $end = ord($top) - ord('A') so scaling stays automatic.
Require a single A–Z character; normalize case if needed.
Proportional fonts hide whether spaces really align.
Pro Tip: if you see ABCCBA, the mirror almost certainly started at the peak instead of i - 1.
Mistakes that commonly break symmetric alphabet pyramids.
Mirroring from i instead of i-1 prints the peak twice.
→ Use $n = $k - 1 then --$n, or loop p = i - 1 .. 0.
Using j >= i shifts the whole pyramid right.
→ Keep for (j = end; j > i; j--).
Char math can walk past the alphabet.
→ Validate a single A–Z letter.
Empty lines or multi-character input can pick the wrong character.
→ Validate after trimming.
All rows glue into one long line.
→ Call echo PHP_EOL after the three stages.
Check these inputs before calling the solution done.
Output is just A (no spaces, no mirror).
Five rows through ABCDEDCBA.
A / ABA / ABCBA (Example 2).
Normalize with strtoupper if needed.
Validate before taking the first character.
Print alpha[k] + " " without changing bounds.
Try these variations to lock in the pattern.
k = i + 1, so $n = $k - 1 restores the peak before --$n.2i + 1 (plus leading spaces).Quick Takeaway: pad for centering, print A..i, mirror from i-1 down to A, then break the line.
| Program | Time | Extra space |
|---|---|---|
| Bridge / input (Examples 1–2) | O(n²) | O(1) (plus alphabet source) |
| Explicit mirror (Example 3) | O(n²) | O(1) |
For n rows each row prints O(n) spaces and letters combined, so total work is O(n²).
The symmetric alphabet pyramid combines centering, ascending letters, and a careful mirror that skips the peak. Master the classic A…ABCDEDCBA sample, then try user input and the explicit-mirror rewrite.
Practice the three examples above, then continue to Program 33’s inverted V-shaped alphabet pattern.
Pad with spaces, print A..i, mirror with $n = $k - 1 and --$n, then break the line.
j > i leading spacesi - 1 down to Aend from the top letterj >= i for paddingecho PHP_EOL inside any of the three stagesPrint the symmetric alphabet pyramid the beginner-friendly way.
Pad + up + mirror
DefinitionA..i
Code(i-1)..A
Code$n = $k-1; --$n
IdiomO(n²) time
AnalysisEach row has three stages: leading spaces for centering, then letters A..i ascending, then letters (i-1)..A descending. The reference keeps a bridge variable $n = $k - 1 after the ascending loop and prints --$n to avoid duplicating the center letter.
Next up: inverted V-shaped alphabet patterns that open downward from a single A at the top.
12 people found this page helpful