Rotating Alphabet Pattern in C

Beginner
⏱️ 7 min read
📚 Updated: Aug 2025
🎯 2 Code Examples
Nested Loops

What You’ll Learn

Each row is a cyclic shift of AE: 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:

Output
ABCDE
BCDEA
CDEBA
DECBA
EDCBA
1

Complete C Program ('A''E')

Character literals; same behavior as loops on 6569.

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

1

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 AE, just rotated so a different letter leads.

Start
2

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.

i..E
3

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.

Rev
4

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 AE, so the rectangle looks uniform.

5
=

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.

2

Variation — User Input

endChar = (char)('A' + rows - 1); second loop uses k > startChar.

c
#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 %c for a spaced layout
  • Generalize with startChar / endChar from input (validate range)
  • Re-implement using modulo indexing on a buffer for longer alphabets

Avoid

  • Printing k in the second loop (duplicates the row’s first letter)
  • Using k >= 'A' with printf("%c", k) without adjusting the forward segment

Key Takeaways

1

Forward segment: endChar - i + 1 letters; wrap segment: i - startChar letters.

2

k - 1 aligns the reverse leg with the letters strictly before i.

3

Row length is constant endChar - startChar + 1; total output for n such rows.

4

O(n²) characters for n rows spanning n letters.

❓ Frequently Asked Questions

The first loop already printed i. The wrap should continue with the letter just before i, which is i - 1 when k == i.
The second loop condition k > 'A' is false immediately, so the row is only the forward run AE.
Program 25 shortens each row and streams a global k++. Here every row has full width and reuses the same letter set in rotated order.
O(n²) printed characters when there are n rows and n letters in the block.

Explore More C Alphabet Patterns!

Forward plus backward segments with a careful boundary (k - 1) is a compact way to express circular shifts.

All Alphabet Patterns →
Did you know?

Reading the letters on a ring A B C D E clockwise from each start gives exactly these five strings.

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