Mirrored Alphabet, Spaces

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

What You’ll Learn

Build rows that widen toward the middle: letters on the left, spaces, then the mirror on the right. The bottom row touches with no space band.

Compare program 18 (palindrome without a gap) and program 15 (stars instead of spaces in the gap).

⭐ Pattern Output

Five rows from A to E (spaces shown as gaps in monospace):

Output
A        A
AB      BA
ABC    CBA
ABCD  DCBA
ABCDEEDCBA
1

Complete C++ Program (Reference Logic)

Two full-width passes. Left prints letters when j <= i; right prints spaces while k > i, then letters.

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

int main() {
    int i, j, k;
    for (i = 65; i <= 69; i++) {
        for (j = 65; j <= 69; j++) {
            if (j <= i) cout << char(j);
            else cout << " ";
        }
        for (k = 69; k >= 65; k--) {
            if (k > i) cout << " ";
            else cout << char(k);
        }
        cout << "\n";
    }
    return 0;
}

🧠 How It Works

1

Outer i

Current peak letter for the row (A through E).

Rows
2

Left ramp

j spans A..E. Print the letter only while j <= i, otherwise print a space.

j <= i
3

Right ramp

k spans E..A. Print spaces while k > i, then print letters when k <= i.

k > i
4

Gap shrinks

The space band is 2 * (endChar - i): each side contributes endChar - i padding slots.

Formula
=

Work per row

Two passes of length n for n letters → O(n²) total.

2

Variation — User Input

startCharendChar replace 'A' and 'E' in both loops.

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

int main() {
    int rows;
    int i, j, k;
    char startChar, endChar;

    cout << "Enter the number of rows: ";
    cin >> rows;

    startChar = 'A';
    endChar = (char)('A' + rows - 1);

    for (i = startChar; i <= endChar; ++i) {
        for (j = startChar; j <= endChar; ++j) {
            if (j <= i) cout << (char) j;
            else cout << " ";
        }
        for (k = endChar; k >= startChar; --k) {
            if (k > i) cout << " ";
            else cout << (char) k;
        }
        cout << "\n";
    }

    return 0;
}

💡 Tips for Enhancement

Try These

  • Swap spaces for . or - to see the gutter clearly
  • Refactor: compute space count 2 * (endChar - i) in one loop
  • Keep rows <= 26 for a single alphabet span

Avoid

  • Mixing up k >= i vs k > i (duplicates or drops the peak on the right)
  • Using different endChar in the two inner loops (breaks alignment)

Key Takeaways

1

Symmetric gutters come from complementary conditions on two full-width passes.

2

j <= i builds the left ramp; k > i pads before the right ramp.

3

Last row: no spaces if i == endChar.

4

O(n²) for n letters in the range.

❓ Frequently Asked Questions

Eight: four from the tail of the first loop (positions after A) and four from the start of the second (before the right A).
When i == endChar, no j or k triggers the space branches, so letters are contiguous.
Program 18 prints one palindrome per row. Here each row is two mirrored halves with a variable gap; they match only when the gap goes to zero on the last row.
O(n²) for n rows over an n-letter span.

Explore More C++ Alphabet Patterns!

Two mirrored halves with a spacer column is the same idea as butterfly or hourglass star programs.

All Alphabet Patterns →
Did you know?

The number of space characters on a row (before the last) is 2 * (endChar - i): each side contributes endChar - i padding slots.

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