A rotating alphabet pattern prints fixed-length rows that start one letter later each time, then finish the row by wrapping earlier letters in reverse.
Remember
Rule: for start i from A to top,
print i..top, then wrap earlier letters reverse
ABCDE
BCDEA
CDEBA
DECBA
EDCBA ← top = 'E' (5 letters each row)
Not a pure left rotation of ABCDE (that would be CDEAB, …). Here the wrap runs backward, so after BCDEA you get CDEBA, then DECBA, then EDCBA. Compare with Program 25 (shrinking sequential stream).
Approach
How to Solve It
Two ways to emit the same shape — nested char loops with a forward + wrap pair, or index into a letter array.
Method
Idea
Best for
Two inner char loops
Forward i..top, then wrap with k-1
Learning, interviews, exams
Char array + indices
Forward slice, then reverse of the prefix
Shorter demos once the idea clicks
Pseudocode
Pseudocode
for start from 'A' to top:
for ch from start to top:
print ch (no newline)
for k from start down to just above 'A':
print (k - 1) (no newline)
print newline
Cheat sheet
Goal
Pattern
Walk each start letter
for (i = 'A'; i <= top; i++)
Forward run
for (j = i; j <= top; j++) printf("%c", j);
Reverse wrap
for (k = i; k > 'A'; k--) printf("%c", k - 1);
End the row
printf("\n");
Avoid duplicate boundary
Print k - 1, never k, in the wrap loop
Array rewrite
Forward letters[i..n-1], then reverse letters[i-1..0]
Printing Letters vs Starting a New Line
API
Effect
Use for
printf("%c", …)
Stays on the same line
Each letter (forward and wrap)
printf("\n")
Ends the current line
After both inner loops
Try it
Live Preview
Change the letter count (top letter = A + n - 1) and the rotating pattern updates instantly — capped at 10 for readability.
Whole numbers from 1 to 10. With n = 5, top is E and you get the classic sample.
Live result5 rows · 25 letters
ABCDE
BCDEA
CDEBA
DECBA
EDCBA
Trace
Worked Walkthrough — top = 'D'
Trace each start letter i with top fixed at 'D' (4 letters per row).
Start i
Forward
Wrap (k-1)
Printed row
'A'
A..D
(none)
ABCD
'B'
B..D
A
BCDA
'C'
C..D
B A
CDBA
'D'
D
C B A
DCBA
Each row prints 4 letters. Total prints: 4 × 4 = 16 = n² for n = 4.
Code
C Programs
Three complete programs: fixed top letter, scanf input, and a char-array rewrite. Use View Output to reveal sample results.
Example 1 — Fixed top at 'E'
Hard-coded range — forward run, then reverse wrap with k - 1.
C
#include <stdio.h>
int main(void)
{
char i, j, k;
for (i = 'A'; i <= 'E'; i++)
{
for (j = i; j <= 'E'; j++)
{
printf("%c", j);
}
for (k = i; k > 'A'; k--)
{
printf("%c", k - 1);
}
printf("\n");
}
return 0;
}
Output
ABCDE
BCDEA
CDEBA
DECBA
EDCBA
How It Works
1. Outer loop picks the start letter.i runs from 'A' to 'E' — one row per start.
2. Forward run. Print i through 'E' (e.g. BCDE when i = 'B').
3. Reverse wrap. Count k down from i toward 'A', but print k - 1 so you do not duplicate i.
4. Break the line.printf("\n") after both inner loops starts the next rotation.
Example 2 — User Input Version
Read the top letter at runtime with scanf. Prefer a leading space in the format and validate A–Z (shown in the tip below).
C
#include <stdio.h>
int main(void)
{
char top, i, j, k;
printf("Enter top letter (like E): ");
scanf(" %c", &top);
for (i = 'A'; i <= top; i++)
{
for (j = i; j <= top; j++)
{
printf("%c", j);
}
for (k = i; k > 'A'; k--)
{
printf("%c", k - 1);
}
printf("\n");
}
return 0;
}
Output (when user enters D)
Enter top letter (like E): D
ABCD
BCDA
CDBA
DCBA
How It Works
1. Prompt and read.scanf(" %c", &top) — the leading space skips leftover newlines.
2. Same rotate core. Only the upper bound changes from hard-coded 'E' to top.
3. Safer input tip. Reject lowercase or non-letters:
Safer input
if (scanf(" %c", &top) != 1 || top < 'A' || top > 'Z')
{
printf("Enter an uppercase letter A–Z.\n");
return 1;
}
Example 3 — Char array indices
Same shape using a string of letters — forward slice, then reverse of the prefix.
C
#include <stdio.h>
int main(void)
{
char letters[] = "ABCDE";
int n = 5;
int i, j, k;
for (i = 0; i < n; i++)
{
for (j = i; j < n; j++)
{
printf("%c", letters[j]);
}
for (k = i - 1; k >= 0; k--)
{
printf("%c", letters[k]);
}
printf("\n");
}
return 0;
}
Output
ABCDE
BCDEA
CDEBA
DECBA
EDCBA
How It Works
1. Forward slice. From index i to the end — same as printing i..top with chars.
2. Reverse prefix. From i - 1 down to 0 — same wrap as printing k - 1, without the off-by-one risk.
3. Learn char loops first. Use Examples 1–2 for exams that want nested char bounds; treat the array rewrite as a polish shortcut.
Edge Cases & Pitfalls
Check these before calling the solution done.
print k
Duplicated boundary
Printing k instead of k - 1 repeats the start letter (e.g. BCDEB instead of BCDEA).
\n early
Broken rows
Call printf("\n") only after both inner loops — not between forward and wrap.
Pure rotate
Wrong wrap direction
A forward wrap of the prefix gives CDEAB, not CDEBA. This pattern wraps in reverse.
top = 'A'
Single A
One row with a single A — forward runs once; the wrap loop never enters.
scanf
Leading space in format
Use scanf(" %c", &top) so a leftover newline is not read as the letter.
lowercase
Validate A–Z
Reject letters outside 'A'..'Z' so the loops stay in the expected range.
Analysis
Time and Space Complexity
Program
Time
Extra space
Char loops (Examples 1–2)
O(n²)
O(1)
Char array (Example 3)
O(n²)
O(n) for the letter buffer
For n letters there are n rows and each row prints n characters — total n² prints.
Remember
Key Takeaways
Rule: forward i..top, then reverse-wrap earlier letters with k - 1.
Fixed length: every row has n letters when the alphabet span is n.
Break the row: call printf("\n") only after both inner loops.
Complexity:O(n²) time; O(1) extra space for nested char loops.
One line: for each start i, print i..top, then wrap earlier letters reverse via k - 1, then printf("\n").
Frequently Asked Questions
The first loop prints from the row start through the top letter. The second prints earlier letters in reverse using k-1 so the boundary letter is not duplicated.
When k equals i, printing k would repeat the first letter of the row. Printing k-1 starts the wrap with the previous letter (e.g. BCDE then A gives BCDEA).
Yes. Forward length plus wrap length equals top-'A'+1, so each row has the same number of letters.
Not exactly. A pure left rotation of ABCDE would give BCDEA, CDEAB, DEABC, EABCD. This pattern wraps earlier letters in reverse, so you get CDEBA, DECBA, EDCBA.
printf("%c", j) prints a letter and stays on the same line. printf("\n") ends the current line. Letters use %c; the row break uses \n after both inner loops.
For n letters, each of n rows prints n characters, so O(n²).
Yes. Index into "ABCDE" (or a longer alphabet) for the forward slice and the reverse of the prefix — Example 3 shows that rewrite.
Use scanf(" %c", &top) with a leading space to skip whitespace, require A–Z, and reject non-letters.
🤔
Did you know?
Outer i is the row start. First inner loop prints i through the top letter. Second inner loop wraps by counting down from i and printing k - 1 so the boundary letter isn’t duplicated. Every row has the same length: top - 'A' + 1 letters.