Shape Rule
Fixed length
ABCDE, BCDEA, CDEBA, … EDCBA.

Each row is a cyclic-style rotation of the same set of letters: print from the row start to E, then wrap by printing the earlier letters in reverse (without duplicating the boundary letter). This output uses adjacent letters (no spaces), matching the reference. Compare Program 25 (shrinking sequential stream) and Program 18 (palindrome halves). Includes a live preview, worked Java examples, edge cases, and complexity.
Fixed length
ABCDE, BCDEA, CDEBA, … EDCBA.
Start..top
First loop prints from row start through E.
k - 1
Earlier letters in reverse; no boundary duplicate.
n letters
Every row prints top - A + 1 characters.
Top letter
Pick a top letter (A–F) and draw the rotations.
Complexity
n rows × n letters each.
A rotating alphabet pattern prints fixed-length rows that start one letter later each time, completing each line by wrapping earlier letters in reverse.
In Java you solve it with nested loops (or string slices): a forward run to the top letter, then a wrap run that uses k - 1 so the join does not duplicate the row start.
It teaches wrap-around indexing and how to join two ranges without a duplicated boundary — useful for rotations and circular buffers.
Print start through top.
Earlier letters in reverse.
Avoids a duplicated join.
Every row has the same length.
In short: for each start i, print i..top, then print earlier letters via k-1 while counting down, then call println().
Given a top letter (or fixed E), print n fixed-length rotating rows from A through that top letter.
// Five rows (adjacent letters; fixed length 5)
// ABCDE
// BCDEA
// CDEBA
// DECBA
// EDCBA | Item | Type | Description |
|---|---|---|
top | char | Last letter in the set (e.g. E). Row count = top - 'A' + 1. |
| Printed output | text | n rows of n adjacent letters each (forward + reverse wrap). |
for i from 0 to n-1: // or 'A'..top
for j from i to n-1: // forward to top
print letter[j]
for k from i down to 1: // wrap earlier letters
print letter[k - 1]
print newline | Approach | Idea | Best for |
|---|---|---|
| Char array + indexes | alpha[j] forward; alpha[k-1] wrap | Matching this classic sample |
| Char loops | j = i..top; wrap with (char)(k-1) | User-chosen top letter |
| String slice + reverse | suffix + reverse(prefix) | Readable rewrite (Example 3) |
| Goal | Pattern |
|---|---|
| Alphabet source | char[] alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray(); |
| Rows | for (int i = 0; i <= 4; i++) (A..E) |
| Forward | for (int j = i; j <= 4; j++) System.out.print(alpha[j]); |
| Wrap | for (int k = i; k > 0; k--) System.out.print(alpha[k - 1]); |
| Shrinking stream | See Program 25 |
Same row — three roles that build the rotation.
forwardLetters from row start through the top
wrapEarlier letters in reverse; no duplicate start
nForward + wrap always totals n letters
breakEnds the row after both loops
Reach for this when teaching wrap-around joins and fixed-length rotations.
Switch from continuous k++ fills to fixed-width rotations.
Practice k-1 so the wrap does not repeat the start letter.
Same shape with array indexes or direct char ranges.
Rewrite with suffix + reverse(prefix) for clarity.
This is a console teaching pattern — not how you build modern app screens.
Key benefit: printing k-1 on the wrap loop is the cleanest way to finish each row without duplicating the boundary letter.
Choose a top letter from A to F and draw the rotating alphabet pattern in the browser.
Three complete Java programs — fixed A–E with a char array, user-chosen top letter, and a string slice rewrite. Click View Output to reveal sample console results.
Print five fixed-length rotating rows from A through E.
Forward run i..E plus wrap run using k - 1.
public class RotatingAlphabet {
public static void main(String[] args) {
char[] alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray();
for (int i = 0; i <= 4; i++) {
for (int j = i; j <= 4; j++)
System.out.print(alpha[j]);
for (int k = i; k > 0; k--)
System.out.print(alpha[k - 1]);
System.out.println();
}
}
} When i = 2 (letter C), forward prints CDE and wrap prints BA via alpha[k-1] → CDEBA. Using k instead of k-1 would wrongly start the wrap with C again.
Let the user choose how many letters to rotate (A..top).
This version uses character loops directly. Prefer validating a single A–Z character from next()/charAt(0) in real apps.
import java.util.Scanner;
public class RotatingAlphabetInput {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter top letter (like E): ");
char top = sc.next().toUpperCase().charAt(0);
for (char i = 'A'; i <= top; i++) {
for (char j = i; j <= top; j++)
System.out.print(j);
for (char k = i; k > 'A'; k--)
System.out.print((char)(k - 1));
System.out.println();
}
sc.close();
}
} Same forward + wrap rules; only the shared top letter changes. Wrap uses (char)(k - 1) so the join stays clean.
Same shape with substring + reverse of the prefix.
Often clearer to read: take the suffix from the start index, then append the reverse of the prefix.
public class RotatingAlphabetString {
public static void main(String[] args) {
String letters = "ABCDE";
for (int i = 0; i < letters.length(); i++) {
String forward = letters.substring(i);
String prefix = new StringBuilder(letters.substring(0, i)).reverse().toString();
System.out.println(forward + prefix);
}
}
} For i = 2, forward is CDE and reversed prefix is BA → CDEBA. Same visual result as the nested-loop versions.
i is the row start (0..4), which corresponds to letters A..E.
Print alpha[i] through alpha[4] (or i..top with char loops).
Count down from k = i and print alpha[k-1]. This appends earlier letters (like A) but doesn’t repeat the row start.
System.out.println() ends the row so the next start index can rotate again.
Each row prints 5 letters, so total output is O(n²) for n letters.
Trace each row’s forward run, reverse wrap, and full line.
i | Forward | Wrap (rev) | Printed row |
|---|---|---|---|
0 (A) | ABCDE | (empty) | ABCDE |
1 (B) | BCDE | A | BCDEA |
2 (C) | CDE | BA | CDEBA |
3 (D) | DE | CBA | DECBA |
4 (E) | E | DCBA | EDCBA |
Every row length is 5. Note the wrap is reverse of the prefix (so row 3 is CDEBA, not CDEAB).
Where this rotating alphabet pattern shows up beyond the homework prompt.
Clearest demo of finishing a row without duplicating the start letter.
Example: print k instead of k-1 and watch the bug.
Fixed-length rotations vs shrinking continuous streams.
Example: print both for n = 5 side by side.
Outer and inner loops over array indexes into the alphabet.
Example: rewrite with char loops (Example 2).
Teach suffix + reverse(prefix) (Example 3).
Example: compare nested loops vs string build.
Fixed n letters per row make O(n²) easy to see.
Example: 5 rows × 5 letters = 25 prints.
Next pattern returns to right-aligned growing prefixes.
Example: continue to Program 27.
Pro Tip: say “forward to the end, then reverse the earlier letters with k-1” before coding — that story prevents a duplicated boundary.
Why this pattern earns a spot after sequential shrinking triangles.
A duplicated join or wrong wrap order shows up immediately.
Array indexes, char loops, or string slices teach the same shape.
A natural place to learn circular-style joins.
Every row has the same width, so tracing stays simple.
Pro Tip: learn the classic nested-loop version first; treat the string-slice rewrite as a clarity upgrade afterward.
Small habits that keep rotating alphabet patterns clean.
That single offset is what prevents a duplicated boundary letter.
Forward + wrap must total top - A + 1 each row.
Require a single A–Z character; normalize case if needed.
Wrap is reverse of the prefix — expect CDEBA, not CDEAB.
Spaces are optional for teaching; the reference has none.
Pro Tip: if you see BCDEB or CDEBC, the wrap loop almost certainly printed k instead of k - 1.
Mistakes that commonly break rotating alphabet patterns.
Duplicates the boundary letter at the wrap join.
→ Always print alpha[k - 1] (or (char)(k - 1)).
You may expect CDEAB but this sample produces CDEBA.
→ Remember the wrap is reverse of the prefix.
Using i < 4 instead of i <= 4 drops the last row.
→ For A..E indexes, loop 0..4 inclusive.
charAt(0)Empty lines or multi-character input throw or take only the first char.
→ Validate a single A–Z letter after trimming.
Using k++ and shrinking lengths builds a different pattern.
→ Keep fixed row length with forward + wrap loops.
Check these inputs before calling the solution done.
Output is just A (wrap empty).
Five rows through EDCBA.
Four rows (Example 2).
Normalize with toUpperCase() if needed.
Validate before charAt(0).
Reject non A–Z tops so loops do not misbehave.
Try these variations to lock in the pattern.
k-1 in reverse.k on the wrap loop for this sample — it duplicates the boundary.k++ stream across shrinking rows.Quick Takeaway: for each start, print forward to the top, wrap earlier letters with k-1, then break the line.
| Program | Time | Extra space |
|---|---|---|
| Array / char loops (Examples 1–2) | O(n²) | O(1) (plus alphabet source) |
| String slice (Example 3) | O(n²) | O(n) per row for temporary strings |
For n letters, each of n rows prints n characters, so total work is O(n²).
The rotating alphabet pattern is a small nested-loop exercise with lasting payoff: fixed-length rows, a forward run, and a reverse wrap joined without a duplicated boundary. Master the classic ABCDE…EDCBA sample, then try user input and the string-slice rewrite.
Practice the three examples above, then continue to Program 27’s right-aligned alphabet pyramid.
Print forward to the top, wrap with k-1, keep fixed row length, then break the line.
k - 1k on the wrap loopCDEAB)println inside the forward or wrap loopPrint the rotating alphabet pattern the beginner-friendly way.
Forward + wrap
DefinitionUse k - 1
CodeFixed per row
CodeEnds each row
I/OO(n²) time
AnalysisOuter i is the row start. First inner loop prints i through E. Second inner loop wraps by counting down from i and printing k - 1 (not k) so the boundary letter isn’t duplicated. Every row prints the same length: E - A + 1 letters.
Next up: right-aligned alphabet pyramids (A, A B, A B C, …).
12 people found this page helpful