C Reverse Alphabet Pattern (Diagonal Star)

Beginner
6 min read
Updated: Sep 2026
3 programs
Live preview

What Is This Pattern?

A reverse alphabet with diagonal star prints each row as letters from the top letter down to A, but replaces exactly one cell with * where the row key equals the column letter (i == j).

Remember
Rule: print j from top..A; when i == j print '*' instead

EDCB*
EDC*A
ED*BA
E*CBA
*DCBA     ← top = E (star slides right → left)

Outer i walks A..E (which letter becomes the star). Inner j walks E..A (what would print in each column). The match slides the star one column left each row.

How to Solve It

Nested char loops plus one equality test — print a star on the diagonal, the column letter everywhere else.

MethodIdeaBest for
Reverse columnsInner j from top..A; star slides right → leftMain pattern, interviews
Forward columnsInner j from A..top; star slides left → rightComparing diagonal directions

Pseudocode

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

Cheat sheet

GoalPattern
Row key (which letter is *)for (i = 'A'; i <= top; i++)
Reverse columnsfor (j = top; j >= 'A'; j--)
Diagonal testif (i == j) printf("*"); else printf("%c", j);
Forward columnsfor (j = 'A'; j <= top; j++)
putchar styleputchar(i == j ? '*' : j);
End the rowprintf("\n");
A–Z-safe topRequire a single letter A–Z

Printing Letters vs Starting a New Line

APIEffectUse for
printf("*") / printf("%c", j)Stays on the same lineEach cell (* or letter)
printf("\n")Ends the current lineAfter the inner column loop

Print each cell without a newline, then end the row once.

Live Preview

Change the top letter and the reverse diagonal-star grid updates instantly — including size and star path.

Enter one letter A–Z. Grid size is n × n where n = top - 'A' + 1.

Live result top E · 5×5 · star right→left
EDCB*
EDC*A
ED*BA
E*CBA
*DCBA

Worked Walkthrough — top = 'E'

Trace each row key i, where i == j fires, and the printed line.

iColumns jStar atPrinted row
AE D C B Arightmost (A)EDCB*
BE D C B Acolumn BEDC*A
CE D C B Acolumn CED*BA
DE D C B Acolumn DE*CBA
EE D C B Aleftmost (E)*DCBA

Exactly one * per row. As i climbs A→E, the match moves left through the reverse letter scan.

C Programs

Three complete programs: fixed top E, top-letter input, and a forward-column contrast. Use View Output to reveal sample results.

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(void)
{
    char 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

1. Outer loop picks the star letter. i takes A, B, C, D, E — the letter that becomes * on that row.

2. Inner loop prints reverse letters. j scans E..A. When i == j, print *; otherwise print j.

3. Star slides left. Row A matches at the right (EDCB*); row E matches at the left (*DCBA).

Example 2 — Top Letter Input

Read the top letter (like E or D) and generate the same pattern for A..top. Check scanf and validate A–Z in real apps.

C
#include <stdio.h>

int main(void)
{
    char top, i, j;

    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

1. Same diagonal rule. Only the bounds follow top instead of the literal 'E'.

2. Leading space in scanf. scanf(" %c", &top) skips leftover whitespace before reading the letter.

3. Safer input tip. Prefer a single uppercase letter:

Safer input
#include <ctype.h>

if (scanf(" %c", &top) != 1)
{
    printf("Enter one letter A–Z.\n");
    return 1;
}
top = (char)toupper((unsigned char)top);
if (top < 'A' || top > 'Z')
{
    printf("Enter one letter A–Z.\n");
    return 1;
}

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(void)
{
    char i, j;

    for (i = 'A'; i <= 'E'; i++)
    {
        for (j = 'A'; j <= 'E'; j++)
        {
            putchar(i == j ? '*' : j);
        }
        putchar('\n');
    }

    return 0;
}

How It Works

1. Same diagonal test. i == j still marks exactly one cell per row.

2. Column order flips. Printing A..E instead of E..A reverses letter order and sends the star left → right.

3. Compare with Example 1. Inner-loop direction controls both the letter sequence and which way the star travels.

Edge Cases & Pitfalls

Check these before calling the solution done.

Wrong compare

No diagonal

Comparing row index to column index (ints) instead of i == j (chars) will not mark the letter diagonal correctly.

Always print j

Missing star

Forgetting the i == j branch prints a plain reverse (or forward) letter square with no asterisk.

\n early

Broken row

Call printf("\n") only after the inner loop. Inside it, each cell lands on its own line.

top = 'A'

Single *

Output is just * on one line — a good sanity check.

Lowercase

Unexpected range

Normalize with toupper so e behaves like E.

Bad scanf

Uninitialized top

Check scanf(" %c", &top) == 1 before looping so a failed read does not leave top garbage.

Time and Space Complexity

ProgramTimeExtra space
Reverse / forward formsO(n²)O(1)

For top letter with offset n = top - 'A' + 1, the nested loops print an n × n grid — quadratic in n.

Key Takeaways

  • Diagonal rule: print * when i == j; otherwise print the column letter.
  • Reverse scan: inner j from top..A sends the star right → left.
  • Direction flip: A..top columns reverse the letter order and the star path.
  • Complexity: O(n²) time for an n × n grid; O(1) extra space.

One line: for each row key i, print reverse letters and replace the cell where i == j with *.

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 top = '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.

Next: Palindromic Alphabet Pyramid

Ascending then descending letters on each centered row.

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