Two Parts
Down then up
Descending prefix + ascending suffix each row.

Build fixed-width alphabet rows from two parts: a short descending prefix (row letter down to B) plus an ascending suffix (A up to a computed cap) — ABCDE, BABCD, CBABC, DCBAB, EDCBA. Compare Program 24 (palindrome split) and Program 26 (rotations). Includes a live preview, worked PHP examples, edge cases, and complexity.
Down then up
Descending prefix + ascending suffix each row.
j > 0
Prefix stops at B so A is not duplicated.
n − i
Ascending ends at 0..(n−i) to keep width n+1.
n+1
For A..E every row has exactly 5 letters.
End letter
Pick an end letter (A–F) and draw the rows.
Complexity
n rows × O(n) letters each.
Decreasing and increasing alphabet rows keep a constant width by trading a growing descending prefix against a shrinking ascending suffix that always starts at A.
In PHP you store the alphabet in an array, walk row index i from 0 to n, print i..1 descending, then print 0..(n-i) ascending.
It teaches composing a row from two opposite loops and choosing a cap so width stays fixed — a skill used in many constant-width letter grids.
i down to B.
A up to the cap.
Prefix skips index 0.
Always n+1 letters.
In short: for each $i from 0 to $n, print $alpha[$i]..$alpha[1], then $alpha[0]..$alpha[$n-$i], then echo PHP_EOL.
Given an end letter (or fixed E), print n+1 fixed-width rows where a descending prefix grows and an ascending suffix shrinks.
// Five rows (end = E, width 5)
// ABCDE
// BABCD
// CBABC
// DCBAB
// EDCBA | Item | Type | Description |
|---|---|---|
end / n | string / int | End letter; $n = ord($end) - ord('A') (4 for E). Rows = n+1. |
| Printed output | text | Fixed-width rows of length n+1 with down+up letter parts. |
$n = ord($end) - ord('A')
for i from 0 to n:
for j from i down to 1: // descending prefix (skip A)
print letter[j]
for k from 0 to n - i: // ascending suffix
print letter[k]
print newline | Approach | Idea | Best for |
|---|---|---|
| Two inner loops | Prefix i..1 then suffix 0..(n-i) | Matching this classic sample |
| Build then reverse-slice | Compose a string per row | When you prefer string ops over char indexes |
| Goal | Pattern |
|---|---|
| Alphabet + n | $alpha = str_split("ABCDEFG..."); $n = 4; |
| Rows | for ($i = 0; $i <= $n; $i++) |
| Descending prefix | for ($j = $i; $j > 0; $j--) echo $alpha[$j]; |
| Ascending suffix | for ($k = 0; $k <= $n - $i; $k++) echo $alpha[$k]; |
| V-shaped next | See Program 31 |
Four roles that keep every row the same width.
prefixDescending letters; skips A
suffixAscending fill from A
capShrinks as the prefix grows
breakEnds the row after both parts
Reach for this when teaching constant-width rows built from opposite letter directions.
Switch from layered floors to fixed-width row composition.
Practice caps that keep every row the same length.
Skip A on the left so the ascending part owns the join.
Work with 0-based alphabet indexes instead of raw chars.
This is a console teaching pattern — not how you build modern app screens.
Key benefit: one descending prefix (skip A) plus a capped ascending suffix is the cleanest way to keep fixed-width down/up alphabet rows.
Choose an end letter from A to F and draw the decreasing/increasing alphabet rows in the browser.
Three complete PHP programs — fixed A–E, user-chosen end letter, and a helper-function rewrite. Click View Output to reveal sample console results.
Print five fixed-width rows from ABCDE down to EDCBA.
Matches the reference logic: print $alpha[$i]…$alpha[1], then $alpha[0]…$alpha[4 - $i].
<?php
$alpha = str_split("ABCDEFGHIJKLMNOPQRSTUVWXYZ");
for ($i = 0; $i <= 4; $i++) {
for ($j = $i; $j > 0; $j--)
echo $alpha[$j];
for ($k = 0; $k <= 4 - $i; $k++)
echo $alpha[$k];
echo PHP_EOL;
} When i = 2, the prefix prints C B and the suffix prints A B C → CBABC. Prefix length + suffix length is always 5.
Let the user pick the end letter (like E).
Works for A..end with the same two-part row. Prefer validating a single A–Z character from the CLI input string in real apps.
<?php
echo "Enter end letter (like E): ";
$end = strtoupper(trim(fgets(STDIN)))[0];
$n = ord($end) - ord('A');
$alpha = str_split("ABCDEFGHIJKLMNOPQRSTUVWXYZ");
for ($i = 0; $i <= $n; $i++) {
for ($j = $i; $j > 0; $j--)
echo $alpha[$j];
for ($k = 0; $k <= $n - $i; $k++)
echo $alpha[$k];
echo PHP_EOL;
} $n = ord($end) - ord('A') scales both loops. For end = C, width is 3 and you get three rows.
Same shape with a shared print_row helper.
Often clearer: one function owns both parts so the caller only walks row indexes.
<?php
function print_row($alpha, $n, $i) {
for ($j = $i; $j > 0; $j--)
echo $alpha[$j];
for ($k = 0; $k <= $n - $i; $k++)
echo $alpha[$k];
echo PHP_EOL;
}
$n = 4;
$alpha = str_split("ABCDEFGHIJKLMNOPQRSTUVWXYZ");
for ($i = 0; $i <= $n; $i++)
print_row($alpha, $n, $i); print_row owns the prefix/suffix pair. The outer loop only decides which row index i to print.
When i = 0 start at A; when i = 4 start at E. Outer loop runs i from 0 to n.
Loop j from i down to 1. This prints the row letter down to B and avoids duplicating A at the join.
Print A through indices 0..(n - i) so total length is always n + 1 (5 for A..E).
Prefix length is i; suffix length is n - i + 1. Together they always equal n + 1.
Left side grows while the right side shrinks — O(n²) time for n letters.
Trace each row index, both parts, and the joined 5-letter line.
i | Prefix (i..1) | Suffix (0..4-i) | Printed row |
|---|---|---|---|
0 | (empty) | ABCDE | ABCDE |
1 | B | ABCD | BABCD |
2 | CB | ABC | CBABC |
3 | DCB | AB | DCBAB |
4 | EDCB | A | EDCBA |
Width is always 4 + 1 = 5. The last row is a full reverse run ending at a single A.
Where these decreasing/increasing alphabet rows show up beyond the homework prompt.
Clearest demo of trading prefix growth against suffix shrink.
Example: count letters each row and confirm width stays 5.
Skip A on the left so the ascending part owns the join.
Example: change j > 0 to j >= 0 and see a double A.
Practice $n = ord($end) - ord('A') with an alphabet array.
Example: scale from E to H without rewriting loops.
Factor both parts into print_row (Example 3).
Example: reuse print_row for spaced output later.
Fixed width × n rows makes O(n²) easy to see.
Example: 5 rows × 5 letters = 25 prints.
Next draws a V using diagonal conditions instead of full rows.
Example: continue to Program 31.
Pro Tip: say “prefix i..B, suffix A..(n-i), width always n+1” before coding — that story prevents a duplicated A at the join.
Why this pattern earns a spot after the reverse-centered pyramid.
A wrong cap or double A shows up immediately in row width.
Down then up is easy to explain and debug.
Change n and every row stays the new width.
print_row keeps the caller short and readable.
Pro Tip: learn the inline loops first; extract print_row once the width budget feels automatic.
Small habits that keep decreasing/increasing rows clean.
Use j > 0 so A is printed only by the suffix.
That bound is what keeps width constant.
Use $n = ord($end) - ord('A') so scaling stays automatic.
Require a single A–Z character; normalize case if needed.
Duplicated prefix/suffix loops are a strong helper signal.
Pro Tip: if a middle row shows ...AA..., the prefix almost certainly included index 0.
Mistakes that commonly break decreasing/increasing alphabet rows.
Duplicates A at the join.
→ Keep for ($j = $i; $j > 0; $j--).
Using $n or $n - $i - 1 breaks constant width.
→ Loop $k from 0 to $n - $i inclusive.
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.
Ascending first then descending produces a different pattern.
→ Keep prefix descending, then suffix ascending.
Check these inputs before calling the solution done.
Output is just A (prefix empty).
5 rows × width 5 through EDCBA.
ABC / BAB / CBA (Example 2).
Normalize with strtoupper if needed.
Validate before taking the first character.
Print alpha[j] + " " without changing bounds.
Try these variations to lock in the pattern.
j >= 0 oncej > 0n + 1 (5 for A..E).Quick Takeaway: print i..B descending, then A..(n-i) ascending, skip duplicating A, then break the line.
| Program | Time | Extra space |
|---|---|---|
| Inline / input (Examples 1–2) | O(n²) | O(1) (plus alphabet source) |
| Helper function (Example 3) | O(n²) | O(1) |
For n+1 letters (indexes 0..n) there are n+1 rows and each row prints n+1 characters, so total work is O(n²).
Decreasing and increasing alphabet rows are a small nested-loop exercise with lasting payoff: opposite letter directions, a join that skips a duplicate A, and a cap that keeps width fixed. Master the classic ABCDE…EDCBA sample, then try user input and the helper rewrite.
Practice the three examples above, then continue to Program 31’s V-shaped alphabet pattern.
Prefix i..1, suffix 0..(n-i), skip duplicating A, then break the line.
j > 0k <= n - in from the end letterecho PHP_EOL inside either part loopPrint decreasing & increasing alphabet rows the beginner-friendly way.
Down then up
Definitioni..1 (skip A)
Code0..(n-i)
CodeAlways n+1
ShapeO(n²) time
AnalysisFor each row i (A..E), print a descending prefix from i down to B (skip A), then print an ascending suffix from A up to A + E - i. That cap keeps row width constant at E - A + 1.
Next up: V-shaped alphabet patterns that print letters only on two diagonals meeting at the bottom vertex.
12 people found this page helpful