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 ('A''E')

Each inner loop runs a fixed five columns; conditions decide letter vs space.

c
#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

1

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.

Rows
2

Left pass: j from 'A' to 'E'

Print the letter when j <= i; otherwise print a space. That draws the ascending ramp Ai with padding on the right side of the left half.

j <= i
3

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.

k > i
4

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.

Fixed
=

Two scans per row

For n letters you do 2n inner iterations per row and n rows → O(n²) output work.

2

Variation — User Input

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

c
#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 <= 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 == 'E', no j or k triggers the space branches, so letters are contiguous.
Program 18 is a single palindrome string per row. Here each row is left padding + right mirror with a variable gap; the shapes match when the gap goes to zero only 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