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 ('A'–'E')
Each inner loop runs a fixed five columns; conditions decide letter vs space.
#include <stdio.h>
int main() {
int i, j, k;
for (i = 'A'; i <= 'E'; ++i) {
for (j = 'A'; j <= 'E'; ++j) {
if (j <= i) {
printf("%c", j);
} else {
printf(" ");
}
}
for (k = 'E'; k >= 'A'; --k) {
if (k > i) {
printf(" ");
} else {
printf("%c", k);
}
}
printf("\n");
}
return 0;
}🧠 How It Works
Outer i is the row’s peak letter
i grows from A to E. That letter is the highest one that appears on the line; everything to its left and right is built with spaces so the row stays centered in a fixed width.
Left pass: j from 'A' to 'E'
Print the letter when j <= i; otherwise print a space. That draws the ascending ramp A…i with padding on the right side of the left half.
Right pass: k from 'E' down to 'A'
Symmetric rule: spaces while k > i, then letters for the descending mirror. Together with the left pass you get a hollow diamond of letters with a space gap in the middle.
Fixed row width
Each pass walks the full alphabet range, so every row emits ten slots (two times five) until the widest row, where spaces disappear in the center and you read a full palindrome across both passes.
Two scans per row
For n letters you do 2n inner iterations per row and n rows → O(n²) output work.
Variation — User Input
startChar … endChar replace 'A' and 'E' in both loops.
#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 = startChar; j <= endChar; ++j) {
if (j <= i) {
printf("%c", j);
} else {
printf(" ");
}
}
for (k = endChar; k >= startChar; --k) {
if (k > i) {
printf(" ");
} else {
printf("%c", k);
}
}
printf("\n");
}
return 0;
}💡 Tips for Enhancement
Try These
- Swap spaces for
.or-to see the gutter clearly - After it works, 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 == 'E', 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
