Reverse Alphabet, Diagonal * in C

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

What You’ll Learn

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

Shape Rule

Reverse + diagonal

Each row is top..A with one cell replaced by *.

Outer Loop

Row key

for (char i = 'A'; i <= top; i++) picks which letter becomes *.

Inner Loop

E down to A

j walks reverse letters; print * when i == j.

Diagonal Test

i == j

One match per row — the star slides left as i grows.

Live Preview

Top letter

Pick a top letter A–Z and draw the grid instantly.

O(n²)

Complexity

An n×n grid prints n characters per row.

Introduction

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.

Why it matters?

It teaches diagonal thinking on a character grid — the same i == j idea used in matrix diagonals, without needing numeric indices.

Key Highlights

Reverse Columns

Inner loop prints top down to A.

Diagonal Match

i == j picks one star per row.

Star Slides Left

As i grows, the match moves left.

n×n Grid

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

📝 Problem & Approach

Given a top letter (like E), print an n×n grid of reverse letters with one diagonal star per row.

c
// Classic sample (top = E)
// EDCB*
// EDC*A
// ED*BA
// E*CBA
// *DCBA

Inputs & Outputs

ItemTypeDescription
topcharHighest letter (e.g. E). Grid size n = top − ‘A’ + 1.
Printed outputtextn rows of reverse letters with one * on the i == j diagonal.

Minimal workflow

Pseudocode
for i from 'A' to top:
    for j from top down to 'A':
        if i == j: print '*'
        else: print j
    print newline

Approach comparison

ApproachIdeaBest for
Char loops + i == jCompare letters directlyMatching this classic sample
Index loopsRows/cols 0..n-1, map to lettersWhen you already think in matrix indices

⚡ Quick Reference

GoalPattern
Row keysfor (i = 'A'; i <= top; ++i)
Reverse columnsfor (j = top; j >= 'A'; --j)
Diagonal starif (i == j) printf("*"); else printf("%c", j);
End the rowprintf("\n");
Forward lettersInner loop j = 'A'..top (Example 3)
Other markerSwap '*' for '#', '@', etc.

📋 Star vs Letter vs printf

Same grid — different roles on each inner-loop pass.

printf("*")
i == j

Diagonal cell for the current row key

printf("%c", j)
else

Reverse letter when not on the diagonal

j--
E..A

Inner direction makes the reverse run

printf("\n")
break

Ends the row after n columns

Context

When This Pattern Shows Up

Reach for this when teaching diagonals on a character grid.

  1. After reverse triangles

    You already print E..A; now mark one cell per row.

  2. Diagonal drills

    Practice i == j without integer indices.

  3. Grid thinking

    See rows and columns as a full rectangle of letters.

  4. Symbol swaps

    Replace letters with markers — useful for puzzle-style labs.

  5. Not a UI layout tool

    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.

🔮 Live Preview

Enter a top letter from A to Z and draw the reverse diagonal-star grid in the browser.

Try E (classic sample) or D (4×4). Use a single letter A–Z.

Live result
Press "Draw pattern".

Examples Gallery

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.

📚 Getting Started

Print the classic 5×5 reverse grid with a sliding star.

Example 1 — Fixed Top E

Outer i runs A..E. Inner j runs E..A. When i == j, print *; otherwise print j.

c
#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;
}

How It Works

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.

📈 Practical Variant

Let the user choose the top letter.

Example 2 — Top Letter Input

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.

c
#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;
}

How It Works

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.

⚡ Direction Variant

Same diagonal test with forward letters.

Example 3 — Forward A..E with Diagonal *

Flip the inner loop to A..E and use putchar. The star now slides left → right on the main diagonal.

c
#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;
}

How It Works

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.

🧠 How the Algorithm Prints Rows

1

Set up

#include <stdio.h> brings in printf / scanf. Choose a top letter (fixed or input).

Setup
2

Outer loop (row key)

i takes A, B, C… top — the letter that becomes * on that row.

Rows
3

Inner loop (reverse)

j runs from top down to A. Default cells print j; the match prints *.

E..A
4

New line

printf("\n") ends the row so the next key starts a fresh line.

Break
=

Grid complete

n letters ⇒ n×n cells — O(n²) time, O(1) extra memory.

🔎 Worked Walkthrough — top E

Trace each outer value of i and see which column becomes the star.

iColumns (E..A)Where i == jPrinted row
AE D C B Alast column (A)EDCB*
BE D C B A4th column (B)EDC*A
CE D C B A3rd column (C)ED*BA
DE D C B A2nd column (D)E*CBA
EE D C B A1st column (E)*DCBA

Exactly one star per row; the match walks from right to left as i increases.

Use Cases

Where this diagonal-star idea shows up beyond the homework prompt.

1. Diagonal Practice

Clearest alphabet demo of i == j on a grid.

Example: print all letters first, then add the star condition.

2. Reverse vs Forward

Flip inner-loop direction to move the star the other way.

Example: compare Examples 1 and 3 side by side.

3. Marker Labs

Swap * for #, @, or a digit.

Example: print row number on the diagonal instead.

4. Matrix Warm-Up

Same idea as marking the main diagonal of a matrix.

Example: later rewrite with integer row/col indices.

5. Complexity Intuition

Full rectangles make O(n²) easy to count.

Example: 5 rows × 5 columns = 25 writes.

6. Char Validation

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.

Advantages

Why this pattern earns a spot after plain reverse letter grids.

  1. 1. Instant Visual Feedback

    Wrong bounds or a missing condition show up as a broken diagonal immediately.

  2. 2. Tiny Condition

    One comparison (i == j) drives the entire special effect.

  3. 3. Easy Variants

    Forward letters, other markers, and input tops are one-line tweaks.

  4. 4. O(1) Extra Memory

    Streaming output needs no storage beyond loop variables.

Pro Tip: master the reverse version first; treat the forward diagonal as a direction flip afterward.

Usage Tips

Small habits that keep diagonal-star code clean.

  1. 1. Keep Matching Bounds

    Outer and inner must share the same letter range or the diagonal will miss.

  2. 2. Decide Direction First

    Reverse (top..A) vs forward (A..top) changes where the star travels.

  3. 3. Validate Letter Input

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

  4. 4. Normalize Case

    char.ToUpperInvariant keeps mixed input consistent with 'A'..top.

  5. 5. Dry-Run Top E

    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.

Common Pitfalls

Mistakes that commonly break reverse diagonal-star patterns.

  1. 1. Mismatched Loop Bounds

    Different ranges for i and j mean some rows never hit i == j.

    → Use the same top for both loops.

  2. 2. Wrong Inner Direction

    Going A..E when you wanted E..A prints a different pattern.

    → Decide reverse vs forward before coding (Examples 1 vs 3).

  3. 3. Comparing Wrong Variables

    Testing i == 'E' or column index alone breaks the sliding diagonal.

    → Compare the row key to the current column letter: i == j.

  4. 4. Unchecked scanf

    Empty or multi-character input can throw or pick the wrong char.

    → Read a string, check length, take [0], validate A–Z.

  5. 5. printf("\n") Inside the Inner Loop

    Breaks the row into one character per line.

    → Call printf("\n") only after the inner loop finishes.

Edge Cases

Check these inputs before calling the solution done.

top = A

Single cell

Output is just * on one line.

top = E

Classic sample

Five rows through *DCBA.

top = D

4×4 grid

Same rule, smaller size (Example 2).

Lowercase

Like e

Normalize to upper, or use 'a'..'e' consistently.

Bad input

Empty scanf

scanf can throw — validate first.

Other mark

# or @

Same loops; only the diagonal character changes.

🎯 Practice Problems

Try these variations to lock in the pattern.

1. Change the marker

  • Print # instead of *
  • Confirm only the diagonal cell changes

2. Forward letters

  • Inner loop A..top (Example 3)
  • Watch the star slide left → right

3. Opposite diagonal

  • Mark where letters “mirror” the row key
  • Hint: think anti-diagonal with indices

4. Continue to Program 18

Notes

  • One star per row. i == j matches exactly once while j walks the shared range.
  • Inner-loop direction controls both letter order and which way the star slides.
  • Keep outer and inner bounds on the same top letter.
  • n letters produce an n×n grid — state O(n²) when asked about complexity.

Quick Takeaway: walk reverse letters, replace the matching column with *, then break the line — that is the whole pattern.

⏱️ Time and Space Complexity

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

Wrap Up

🎉 Conclusion

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.

💡 Best Practices

✅ Do

  • Share the same top letter for outer and inner loops
  • Use i == j for the diagonal star
  • Decide reverse vs forward before coding
  • Validate a single A–Z character on input
  • State O(n²) when asked about complexity

❌ Don’t

  • Use mismatched ranges for i and j
  • Call printf("\n") inside the column loop
  • Assume empty input is safe for scanf
  • Forget that inner direction changes star travel
  • Mix lowercase and uppercase without normalizing

Key Takeaways

Knowledge Unlocked

Five things to remember about this alphabet pattern

Print the reverse diagonal-star grid the beginner-friendly way.

5
Core concepts
02

Inner loop

top down to A

Code
= 03

Diagonal

i == j → *

Code
04

New line

Ends each row

I/O
O 05

Complexity

O(n²) time

Analysis

❓ Frequently Asked Questions

i walks A..E down the rows while j scans E..A across columns. The condition i == j marks one diagonal position per row, which gets replaced by '*'.
Because i increases each row, but the printed letters go from E down to A across the row. The match i==j occurs at a different column each time, sliding the star left.
The star prints when i == j, so each row replaces exactly one character at the matching column.
Yes. Replace '*' with any symbol (like '#' or '@') in the conditional branch.
The first two print a cell and stay on the same line. printf("\n") ends the row after the inner loop finishes.
You print forward letters instead of E..A, and the star slides the other way (left to right on the main diagonal).
O(n²) for an n×n letter grid because every row prints n characters.
Use scanf(" %c", &top) (leading space skips whitespace), require A–Z, and reject bad input. Or read rows with scanf("%d", &rows) and set endChar = 'A' + rows - 1.

Did you Know? 🔊

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

Continue to Alphabet Pattern 18

Next up: a palindromic alphabet pyramid.

Program 18 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