C++ Rotating Alphabet Pattern

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

What Is This Pattern?

A rotating alphabet pattern reprints the same letter set each row, starting one letter further along and wrapping earlier letters in reverse so every line stays the same length.

Remember
Rule: for start i, print i..top, then reverse wrap of A..(i-1)

ABCDE
BCDEA
CDEBA
DECBA
EDCBA     ← top = 'E' (5 letters each row)

Not a pure left rotation: after BCDEA you get CDEBA (reverse wrap), not CDEAB. Print k - 1 on the wrap loop so the row-start letter is not duplicated.

How to Solve It

Two ways to emit the same rows — nested index/char loops, then optionally a suffix + reverse-prefix rewrite.

MethodIdeaBest for
Forward + wrapPrint i..top, then wrap with k - 1Learning, interviews, exams
String sliceSuffix from i + reverse of prefixReadable rewrite once the idea clicks

Pseudocode

Pseudocode
for i from 'A' to top:
    for j from i to top:
        print j
    for k from i down to 'B':
        print (k - 1)
    print newline

Cheat sheet

GoalPattern
Letter tableconst char* alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
Row startsfor (int i = 0; i <= 4; i++) (A..E)
Forward runfor (int j = i; j <= 4; j++) cout << alpha[j];
Reverse wrapfor (int k = i; k > 0; k--) cout << alpha[k - 1];
End the rowcout << "\n";
Row lengthtop - 'A' + 1 (same on every row)
Avoid double startPrint k - 1, not k, on the wrap loop

Printing Letters vs Starting a New Line

APIEffectUse for
cout << chStays on the same lineEach letter on forward and wrap
cout << "\n"Ends the current lineAfter both inner loops

Print letters without a newline, then end the row once.

Live Preview

Change the top letter and the rotating rows update instantly — every line stays the same length.

One letter from A to F. Tap a chip or type a letter — the preview redraws as you go.

Live result Top E · 5 rows · 5 letters/row
ABCDE
BCDEA
CDEBA
DECBA
EDCBA

Worked Walkthrough — A–E

Trace each row’s forward run, reverse wrap, and full line.

iForwardWrap (rev)Printed row
0 (A)ABCDE(empty)ABCDE
1 (B)BCDEABCDEA
2 (C)CDEBACDEBA
3 (D)DECBADECBA
4 (E)EDCBAEDCBA

Every row length is 5. Note the wrap is the reverse of the prefix — row 3 is CDEBA, not CDEAB.

C++ Programs

Three complete programs: fixed A–E, top-letter input, and a string-slice rewrite. Use View Output to reveal sample results.

Example 1 — Fixed A–E

Forward run i..E plus wrap run using k - 1.

C++
#include <iostream>
using namespace std;

int main()
{
    const char* alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

    for (int i = 0; i <= 4; i++)
    {
        for (int j = i; j <= 4; j++)
        {
            cout << alpha[j];
        }

        for (int k = i; k > 0; k--)
        {
            cout << alpha[k - 1];
        }

        cout << "\n";
    }

    return 0;
}

How It Works

1. Index the alphabet. alpha[0] is A, alpha[4] is E.

2. Outer loop picks the start. i runs from 0 to 4 — one start letter per row.

3. Forward run. Print alpha[j] from i through 4.

4. Reverse wrap. Count k down from i and print alpha[k - 1] so the start letter is not repeated.

When i = 2 (letter C), forward prints CDE and wrap prints BA → CDEBA.

Example 2 — Top Letter Input

Same forward + wrap rules with character loops. Prefer validating a single A–Z character in real apps.

C++
#include <iostream>
using namespace std;

int main()
{
    char top;
    cout << "Enter top letter (like E): ";
    cin >> top;

    for (char i = 'A'; i <= top; i++)
    {
        for (char j = i; j <= top; j++)
        {
            cout << j;
        }

        for (char k = i; k > 'A'; k--)
        {
            cout << (char)(k - 1);
        }

        cout << "\n";
    }

    return 0;
}

How It Works

1. Prompt and read. Ask for a top letter, then read it with cin.

2. Same wing rules. Forward is i..top; wrap uses (char)(k - 1) so the join stays clean.

3. Safer input tip. Prefer:

Safer input
#include <cctype>

if (!(cin >> top))
{
    cout << "Enter one letter from A to Z.\n";
    return 1;
}
top = (char)toupper((unsigned char)top);
if (top < 'A' || top > 'Z')
{
    cout << "Enter one letter from A to Z.\n";
    return 1;
}

Example 3 — String Slice Rewrite

Often clearer to read: take the suffix from the start index, then append the reverse of the prefix.

C++
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;

int main()
{
    string letters = "ABCDE";

    for (size_t i = 0; i < letters.size(); i++)
    {
        string forward = letters.substr(i);
        string prefix = letters.substr(0, i);
        reverse(prefix.begin(), prefix.end());

        cout << forward << prefix << "\n";
    }

    return 0;
}

How It Works

1. Slice the suffix. letters.substr(i) is the forward run from the start index.

2. Reverse the prefix. Letters before i are reversed and appended — same wrap as Examples 1–2.

3. One line print. Concatenate and print the full row in a single statement.

For i = 2, forward is CDE and reversed prefix is BA → CDEBA.

Edge Cases & Pitfalls

Check these before calling the solution done.

Print k

Duplicate start letter

If the wrap loop prints k instead of k - 1, rows like BCDEB repeat the start. Always use k - 1.

Forward wrap

Pure left rotation

Appending the prefix in order gives CDEAB, not CDEBA. This pattern reverses the wrap.

\n early

Column of letters

If cout << "\n" is inside either inner loop, each letter lands on its own line. Print letters without a newline; end the row only after both loops.

top = A

Single letter

Output is just A — wrap never runs. A good sanity check.

top < A

Empty output

Outer loop never runs if top < 'A'. Validate A–Z before looping.

Bad cin

Validate one letter

Check cin >> top, optionally uppercase, and require A–Z before the outer loop.

Time and Space Complexity

ProgramTimeExtra space
Forward + wrap (Examples 1–2)O(n²)O(1) (plus the fixed alphabet table)
String slice (Example 3)O(n²)O(n) per temporary row string

For n = top - 'A' + 1 letters, each of n rows prints n characters — still quadratic in n.

Key Takeaways

  • Rule: for start i, print i..top, then reverse wrap of A..(i-1).
  • No duplicate start: wrap with k - 1, not k.
  • Break the row: call cout << "\n" only after both loops.
  • Complexity: O(n²) time; O(1) extra space for nested loops.

One line: for each start i, print i..top, then the reverse of A..(i-1), then cout << "\n".

Frequently Asked Questions

The first loop prints from the row start through E (or your top letter). The second prints the letters before the start (wrapping) 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).
Each row prints from the row start to the end, then prints earlier letters in reverse to complete the row. That creates the rotation effect.
Yes. Forward length plus wrap length equals E-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.
For n letters, each of n rows prints n characters, so O(n²).
Use cin >> top, require A–Z (optionally toupper), and reject failed input.
Yes. Index into "ABCDEFG..." or build each row with substr + reverse of the prefix — Example 3 shows a string-based rewrite.

Did you know?

Outer 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: Right-Aligned Pyramid

Move from fixed-length rotating rows to a growing A B C pyramid with leading spaces.

Program 27 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