Shape Rule
Reverse + diagonal
Each row is top..A with one cell replaced by *.

Print the reverse alphabet line EDCBA on every row, but replace one character per row with * where i == j. The star slides from right to left: EDCB*, EDC*A, ED*BA, E*CBA, *DCBA. Includes a live preview, worked C examples, edge cases, and complexity. Next: Program 18 (palindromic pyramid).
Reverse + diagonal
Each row is top..A with one cell replaced by *.
Row key
for (char i = 'A'; i <= top; i++) picks which letter becomes *.
E down to A
j walks reverse letters; print * when i == j.
i == j
One match per row — the star slides left as i grows.
Top letter
Pick a top letter A–Z and draw the grid instantly.
Complexity
An n×n grid prints n characters per row.
A reverse alphabet diagonal-star pattern prints the same reverse letter run on every row (for example EDCBA), then swaps exactly one cell for * where the row key equals the column letter.
In C you solve it with nested loops: outer i walks A..top, inner j walks top..A, and a simple i == j test decides star vs letter.
It teaches diagonal thinking on a character grid — the same i == j idea used in matrix diagonals, without needing numeric indices.
Inner loop prints top down to A.
i == j picks one star per row.
As i grows, the match moves left.
n letters ⇒ n rows × n columns.
In short: for each row key i, scan columns with reverse j; print * when i == j, else print j, then printf("\n").
Given a top letter (like E), print an n×n grid of reverse letters with one diagonal star per row.
// Classic sample (top = E)
// EDCB*
// EDC*A
// ED*BA
// E*CBA
// *DCBA | Item | Type | Description |
|---|---|---|
top | char | Highest letter (e.g. E). Grid size n = top − ‘A’ + 1. |
| Printed output | text | n rows of reverse letters with one * on the i == j diagonal. |
for i from 'A' to top:
for j from top down to 'A':
if i == j: print '*'
else: print j
print newline | Approach | Idea | Best for |
|---|---|---|
Char loops + i == j | Compare letters directly | Matching this classic sample |
| Index loops | Rows/cols 0..n-1, map to letters | When you already think in matrix indices |
| Goal | Pattern |
|---|---|
| Row keys | for (i = 'A'; i <= top; ++i) |
| Reverse columns | for (j = top; j >= 'A'; --j) |
| Diagonal star | if (i == j) printf("*"); else printf("%c", j); |
| End the row | printf("\n"); |
| Forward letters | Inner loop j = 'A'..top (Example 3) |
| Other marker | Swap '*' for '#', '@', etc. |
Same grid — different roles on each inner-loop pass.
i == jDiagonal cell for the current row key
elseReverse letter when not on the diagonal
E..AInner direction makes the reverse run
breakEnds the row after n columns
Reach for this when teaching diagonals on a character grid.
You already print E..A; now mark one cell per row.
Practice i == j without integer indices.
See rows and columns as a full rectangle of letters.
Replace letters with markers — useful for puzzle-style labs.
This is a console teaching pattern — not how you build modern app screens.
Key benefit: one tiny condition (i == j) turns a flat reverse grid into a moving diagonal.
Enter a top letter from A to Z and draw the reverse diagonal-star grid in the browser.
Three complete C programs — fixed top E, user-chosen top letter, and a forward A..E diagonal variant. Click View Output to reveal sample console results.
Print the classic 5×5 reverse grid with a sliding star.
EOuter i runs A..E. Inner j runs E..A. When i == j, print *; otherwise print j.
#include <stdio.h>
int main() {
int i, j;
for (i = 'A'; i <= 'E'; ++i) {
for (j = 'E'; j >= 'A'; --j) {
if (i == j) {
printf("*");
} else {
printf("%c", j);
}
}
printf("\n");
}
return 0;
} Row i = 'A' matches when j reaches A (rightmost column) → EDCB*. Row i = 'B' matches one column earlier → EDC*A. By i = 'E', the star is at the leftmost column → *DCBA.
Let the user choose the top letter.
Read the top letter (like E or D) and generate the same pattern for A..top. Check scanf(" %c", &top) and validate A–Z in real apps.
#include <stdio.h>
int main() {
int i, j;
char top;
printf("Enter the top letter (like E): ");
scanf(" %c", &top);
for (i = 'A'; i <= top; ++i) {
for (j = top; j >= 'A'; --j) {
if (i == j) {
printf("*");
} else {
printf("%c", j);
}
}
printf("\n");
}
return 0;
} Same i == j diagonal; only the bounds follow top. With top = 'D' you get a 4×4 grid and the star still slides right → left.
Same diagonal test with forward letters.
Flip the inner loop to A..E and use putchar. The star now slides left → right on the main diagonal.
#include <stdio.h>
int main() {
int i, j;
for (i = 'A'; i <= 'E'; ++i) {
for (j = 'A'; j <= 'E'; ++j) {
if (i == j) {
putchar('*');
} else {
putchar(j);
}
}
putchar('\n');
}
return 0;
} The diagonal rule is unchanged — only column order flips. Comparing Examples 1 and 3 shows how inner-loop direction controls both letter order and which way the star travels.
#include <stdio.h> brings in printf / scanf. Choose a top letter (fixed or input).
i takes A, B, C… top — the letter that becomes * on that row.
j runs from top down to A. Default cells print j; the match prints *.
printf("\n") ends the row so the next key starts a fresh line.
n letters ⇒ n×n cells — O(n²) time, O(1) extra memory.
ETrace each outer value of i and see which column becomes the star.
i | Columns (E..A) | Where i == j | Printed row |
|---|---|---|---|
A | E D C B A | last column (A) | EDCB* |
B | E D C B A | 4th column (B) | EDC*A |
C | E D C B A | 3rd column (C) | ED*BA |
D | E D C B A | 2nd column (D) | E*CBA |
E | E D C B A | 1st column (E) | *DCBA |
Exactly one star per row; the match walks from right to left as i increases.
Where this diagonal-star idea shows up beyond the homework prompt.
Clearest alphabet demo of i == j on a grid.
Example: print all letters first, then add the star condition.
Flip inner-loop direction to move the star the other way.
Example: compare Examples 1 and 3 side by side.
Swap * for #, @, or a digit.
Example: print row number on the diagonal instead.
Same idea as marking the main diagonal of a matrix.
Example: later rewrite with integer row/col indices.
Full rectangles make O(n²) easy to count.
Example: 5 rows × 5 columns = 25 writes.
Practice reading and validating a single letter input.
Example: reject empty strings and non A–Z input.
Pro Tip: say “print reverse letters, replace the match with a star” before coding — that story prevents wrong loop bounds.
Why this pattern earns a spot after plain reverse letter grids.
Wrong bounds or a missing condition show up as a broken diagonal immediately.
One comparison (i == j) drives the entire special effect.
Forward letters, other markers, and input tops are one-line tweaks.
Streaming output needs no storage beyond loop variables.
Pro Tip: master the reverse version first; treat the forward diagonal as a direction flip afterward.
Small habits that keep diagonal-star code clean.
Outer and inner must share the same letter range or the diagonal will miss.
Reverse (top..A) vs forward (A..top) changes where the star travels.
Require a single A–Z character; empty scanf breaks scanf.
char.ToUpperInvariant keeps mixed input consistent with 'A'..top.
Trace one star position per row on paper before coding larger tops.
Pro Tip: if every row prints a full reverse line with no star, you almost certainly forgot the i == j branch.
Mistakes that commonly break reverse diagonal-star patterns.
Different ranges for i and j mean some rows never hit i == j.
→ Use the same top for both loops.
Going A..E when you wanted E..A prints a different pattern.
→ Decide reverse vs forward before coding (Examples 1 vs 3).
Testing i == 'E' or column index alone breaks the sliding diagonal.
→ Compare the row key to the current column letter: i == j.
scanfEmpty or multi-character input can throw or pick the wrong char.
→ Read a string, check length, take [0], validate A–Z.
printf("\n") Inside the Inner LoopBreaks the row into one character per line.
→ Call printf("\n") only after the inner loop finishes.
Check these inputs before calling the solution done.
Output is just * on one line.
Five rows through *DCBA.
Same rule, smaller size (Example 2).
Normalize to upper, or use 'a'..'e' consistently.
scanf can throw — validate first.
Same loops; only the diagonal character changes.
Try these variations to lock in the pattern.
# instead of *i == j matches exactly once while j walks the shared range.Quick Takeaway: walk reverse letters, replace the matching column with *, then break the line — that is the whole pattern.
| Program | Time | Extra space |
|---|---|---|
| Fixed / input reverse diagonal (Examples 1–2) | O(n²) | O(1) |
| Forward diagonal (Example 3) | O(n²) | O(1) |
With n = top − ‘A’ + 1, every row prints n characters, so total work is O(n²).
The reverse alphabet diagonal-star pattern is a small nested-loop exercise with lasting payoff: reverse column order, a shared letter range, and one diagonal test. Master the classic EDCB* sample, then try user input and the forward-letter variant.
Practice the three examples above, then continue to Program 18’s palindromic alphabet pyramid.
Use matching A..top bounds, print reverse letters, swap * when i == j, and break only after the inner loop.
i == j for the diagonal stari and jprintf("\n") inside the column loopscanfPrint the reverse diagonal-star grid the beginner-friendly way.
Reverse + diagonal *
Definitiontop down to A
Codei == j → *
CodeEnds each row
I/OO(n²) time
AnalysisThe diagonal is defined by i == j while the row prints letters from E down to A. Since i increases A..E each row, the star moves one position left each line: EDCB*, EDC*A, ED*BA, E*CBA, *DCBA.
Next up: a palindromic alphabet pyramid.
12 people found this page helpful