Mirrored Alphabet, Spaces

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):
A A
AB BA
ABC CBA
ABCD DCBA
ABCDEEDCBAComplete C++ Program (Reference Logic)
Two full-width passes. Left prints letters when j <= i; right prints spaces while k > i, then letters.
#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
Outer i
Current peak letter for the row (A through E).
Left ramp
j spans A..E. Print the letter only while j <= i, otherwise print a space.
Right ramp
k spans E..A. Print spaces while k > i, then print letters when k <= i.
Gap shrinks
The space band is 2 * (endChar - i): each side contributes endChar - i padding slots.
Work per row
Two passes of length n for n letters → O(n²) total.
Variation — User Input
startChar … endChar replace 'A' and 'E' in both loops.
#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 <= 26for a single alphabet span
Avoid
- Mixing up
k >= ivsk > i(duplicates or drops the peak on the right) - Using different
endCharin the two inner loops (breaks alignment)
Key Takeaways
Symmetric gutters come from complementary conditions on two full-width passes.
j <= i builds the left ramp; k > i pads before the right ramp.
Last row: no spaces if i == endChar.
O(n²) for n letters in the range.
❓ Frequently Asked Questions
A) and four from the start of the second (before the right A).i == endChar, no j or k triggers the space branches, so letters are contiguous.Explore More C++ Alphabet Patterns!
Two mirrored halves with a spacer column is the same idea as butterfly or hourglass star programs.
The number of space characters on a row (before the last) is 2 * (endChar - i): each side contributes endChar - i padding slots.
12 people found this page helpful
