Rotating Alphabet Pattern in C

What You’ll Learn
Each row is a cyclic shift of A…E: print from the row letter through E, then continue backward through the letters before the start. Contrast program 25, where row length shrinks and a single k++ walks forward only.
Output uses adjacent %c (no spaces), matching the reference.
⭐ Pattern Output
Five letters per row, five rows:
ABCDE
BCDEA
CDEBA
DECBA
EDCBAComplete C Program ('A'–'E')
Character literals; same behavior as loops on 65…69.
#include <stdio.h>
int main() {
int 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;
}🧠 How It Works
Outer i is the rotation start
Each row chooses a new first letter from A to E. Everything printed on that row is still a permutation of the same block A…E, just rotated so a different letter leads.
Forward leg: j from i to E
The first inner loop prints the suffix of the alphabet from the row’s start through the top letter. For i = 'B' you emit BCDE before the wrap segment begins.
Wrap leg: k counts down with k - 1
Initialize k = i. While k > 'A', print k - 1 and decrement k. That appends i-1, i-2, … A without printing the starting letter twice.
Why every row length is E - A + 1
Forward part prints E - i + 1 letters; reverse part prints i - A letters. Their sum simplifies to five characters for A…E, so the rectangle looks uniform.
Cyclic shifts
Each row is a rotation of ABCDE. Two inner passes per outer row give O(n²) prints for n letters in the block.
Variation — User Input
endChar = (char)('A' + rows - 1); second loop uses k > startChar.
#include <stdio.h>
int main() {
int rows;
int i, j, k;
char startChar, endChar;
printf("Enter the number of rows: ");
scanf("%d", &rows);
startChar = 'A';
endChar = (char)('A' + rows - 1);
for (i = startChar; i <= endChar; ++i) {
for (j = i; j <= endChar; ++j) {
printf("%c", j);
}
for (k = i; k > startChar; --k) {
printf("%c", k - 1);
}
printf("\n");
}
return 0;
}💡 Tips for Enhancement
Try These
- Insert a space after each
%cfor a spaced layout - Generalize with
startChar/endCharfrom input (validate range) - Re-implement using modulo indexing on a buffer for longer alphabets
Avoid
- Printing
kin the second loop (duplicates the row’s first letter) - Using
k >= 'A'withprintf("%c", k)without adjusting the forward segment
Key Takeaways
Forward segment: endChar - i + 1 letters; wrap segment: i - startChar letters.
k - 1 aligns the reverse leg with the letters strictly before i.
Row length is constant endChar - startChar + 1; total output n² for n such rows.
O(n²) characters for n rows spanning n letters.
❓ Frequently Asked Questions
i. The wrap should continue with the letter just before i, which is i - 1 when k == i.k > 'A' is false immediately, so the row is only the forward run A…E.k++. Here every row has full width and reuses the same letter set in rotated order.Explore More C Alphabet Patterns!
Forward plus backward segments with a careful boundary (k - 1) is a compact way to express circular shifts.
Reading the letters on a ring A B C D E clockwise from each start gives exactly these five strings.
12 people found this page helpful
