C++ Mirrored Alphabet Pattern (Spaced)

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

What Is This Pattern?

A mirrored alphabet with spaces prints a growing left ramp and a matching right mirror, separated by a shrinking band of spaces until the last row meets as ABCDEEDCBA.

Remember
Rule: left A..i + spaces + right i..A (gap shrinks)

A        A
AB      BA
ABC    CBA
ABCD  DCBA
ABCDEEDCBA     ← top = E

Unlike Program 18 (a continuous palindrome with no gap), this pattern keeps a middle space band that shrinks by two columns each row until both halves touch.

How to Solve It

For each peak, print a left half, a space gap, and a mirrored right half — all aligned to a fixed total width.

MethodIdeaBest for
Dual fixed scanLeft: letter if j <= i else space; right: space if k > i else letterLearning column conditions
Explicit gapPrint left letters, 2*(n-i) spaces, then reverse lettersClearer reading once the shape is clear

Pseudocode

Pseudocode
for i from 'A' to top:
    for j from 'A' to top:
        print j if j <= i else space
    for k from top down to 'A':
        print space if k > i else k
    print newline

Cheat sheet

GoalPattern
Grow the peakfor (char i = 'A'; i <= top; i++)
Left rampif (j <= i) cout << j; else cout << " ";
Right rampif (k > i) cout << " "; else cout << k;
Explicit gapfor (int s = 0; s < 2 * (n - i); s++) cout << " ";
End the rowcout << "\n";
Last-row meetWhen i == top, gap is 0 → ABCDEEDCBA

Printing Letters vs Starting a New Line

APIEffectUse for
cout << ch / cout << " "Stays on the same lineEach letter and each space cell
cout << "\n"Ends the current lineAfter left + right (or left + gap + right)

Print each cell without a newline, then end the row once.

Live Preview

Change the top letter and the mirrored space pattern updates instantly — including half-width and gap size.

Enter one letter A–J. Half-width is n = top - 'A'; each row is width 2 × (n + 1).

Live result top E · width 10 · 5 rows
A        A
AB      BA
ABC    CBA
ABCD  DCBA
ABCDEEDCBA

Worked Walkthrough — top = 'E' (n = 4)

Trace each peak index, the left half, the gap, and the mirrored right half.

iLeftGapRightPrinted row
0A8AA A
1AB6BAAB BA
2ABC4CBAABC CBA
3ABCD2DCBAABCD DCBA
4ABCDE0EDCBAABCDEEDCBA

Gap size is 2*(n - i). On the last row the halves meet and the peak letter appears twice (EE).

C++ Programs

Three complete programs: fixed A–E dual scan, top-letter input, and an explicit gap form. Use View Output to reveal sample results.

Example 1 — Fixed A–E

Two fixed-width scans per row. Conditions decide whether to print a letter or a space.

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

int main()
{
    for (char i = 'A'; i <= 'E'; i++)
    {
        for (char j = 'A'; j <= 'E'; j++)
        {
            if (j <= i)
            {
                cout << j;
            }
            else
            {
                cout << " ";
            }
        }

        for (char k = 'E'; k >= 'A'; k--)
        {
            if (k > i)
            {
                cout << " ";
            }
            else
            {
                cout << k;
            }
        }

        cout << "\n";
    }

    return 0;
}

How It Works

1. Outer loop grows the peak. i runs A..E — how many letters appear on each wing.

2. Left scan. Print j while j <= i; otherwise print a space to fill the half-width.

3. Right scan. Print spaces while k > i, then print descending letters. When i = 'E', every column is a letter → ABCDEEDCBA.

Example 2 — Top Letter Input

Build the full width dynamically from the chosen top letter. Check cin and validate A–Z in real apps.

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

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

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

        for (char k = top; k >= 'A'; k--)
        {
            if (k > i)
            {
                cout << " ";
            }
            else
            {
                cout << k;
            }
        }

        cout << "\n";
    }

    return 0;
}

How It Works

1. Bounds follow top. Both loops scan 'A'..top instead of the literal 'E'.

2. Same dual-scan core. Left and right conditions match Example 1 — only the width changes.

3. Safer input tip. Prefer a single uppercase letter:

Safer input
#include <cctype>

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

Example 3 — Letters, Gap Count, Mirror

Often clearer to read: print left letters, print 2*(n-i) spaces, then print the reverse letters.

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

int main()
{
    int n = 4; /* last index (E) */

    for (int i = 0; i <= n; i++)
    {
        for (int j = 0; j <= i; j++)
        {
            cout << (char)('A' + j);
        }

        for (int s = 0; s < 2 * (n - i); s++)
        {
            cout << " ";
        }

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

        cout << "\n";
    }

    return 0;
}

How It Works

1. Left letters only. Print A..i without padding inside the half.

2. Explicit gap. 2*(n - i) spaces replace the leftover columns from both fixed scans.

3. Mirror from the peak. Print i..A. On the last row the gap is 0, so the peak letter appears twice.

Edge Cases & Pitfalls

Check these before calling the solution done.

Gap = n - i

Half-width gap

Forgetting the 2 * in the explicit form leaves only half the middle spaces — the mirror shifts left.

Right from i-1

Missing peak twin

Starting the right half at i - 1 removes the doubled center on the last row. Only do that if the problem asks for it.

\n early

Broken row

Call cout << "\n" only after both halves. Inside any inner loop, each cell lands on its own line.

top = 'A'

Single AA

Output is AA (no gap) — a good sanity check.

No spaces

Wrong pattern

Skipping the space branches collapses into a continuous palindrome each row — that is Program 18, not this one.

Bad cin

Failed stream

Check cin >> top before looping so failed or empty input does not leave top garbage.

Time and Space Complexity

ProgramTimeExtra space
Dual scan / explicit gapO(n²)O(1)

For half-width n + 1 letters, each of n + 1 rows prints 2(n + 1) characters — quadratic in the letter count.

Key Takeaways

  • Three parts: left ramp, shrinking space gap, right mirror.
  • Gap formula: 2*(n - i) spaces between the halves.
  • Final meet: last row has gap 0 and a doubled peak letter.
  • Complexity: O(n²) time; O(1) extra space.

One line: for each peak, print left letters, a shrinking even gap, then the mirrored right letters.

Frequently Asked Questions

The left loop builds the increasing part and fills the remaining columns with spaces. The right loop fills spaces until the peak, then prints the decreasing mirror.
The spaces keep both halves fixed width so the mirror effect is aligned. The gap shrinks each row until both halves touch.
When i reaches the last letter (E), all positions satisfy the letter conditions on both sides, so both halves print letters and meet as ABCDEEDCBA.
Increase the last index/letter and update the loop bounds so the left and right halves each scan the new width.
The first forms print a cell and stay on the same line. cout << "\n" ends the row after both halves finish.
Program 18 prints a continuous palindrome with no middle gap. This pattern keeps a shrinking space band between left and right ramps until the final row.
O(n²) for n letters because each row scans n columns twice (left + right).
Use cin >> top, require A–Z (optionally toupper), and reject failed input.

Did you know?

Each row uses two fixed-width scans from A to E. The first builds the left ramp (letters when j <= i else spaces). The second builds the right ramp (spaces while k > i, else letters). The gap shrinks until the last row meets as ABCDEEDCBA.

Next: Right-Aligned Reverse

Leading spaces plus descending letters on each row.

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