Mirrored Alphabet, Spaces in C#

What You’ll Learn
Build rows that widen toward the middle: letters on the left, a shrinking space band, then the mirrored letters on the right.
Compare Program 18 (palindrome without a gap) and Program 15 (stars in the middle).
⭐ 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)
Two fixed-width scans per row. Conditions decide whether to print a letter or a space.
using System;
namespace MyApp
{
class Program
{
static void Main(string[] args)
{
char[] alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray();
for (int i = 0; i <= 4; i++)
{
for (int j = 0; j <= 4; j++)
{
if (j <= i)
Console.Write(alpha[j]);
else
Console.Write(" ");
}
for (int k = 4; k >= 0; k--)
{
if (k > i)
Console.Write(" ");
else
Console.Write(alpha[k]);
}
Console.WriteLine();
}
}
}
}🧠 How It Works
Fix the width
Both halves scan a fixed range (0..4), so each row has a consistent total width.
Paint the left ramp
For column j, print alpha[j] if j <= i, else print a space.
Paint the right ramp
Scan from the end: while k > i print spaces; once k <= i, print alpha[k] to mirror the left.
Gap shrinks each row
On the last row (E), both halves print only letters, meeting as ABCDEEDCBA.
Variation — User Input (Top Letter)
Let the user choose the last letter (like E). The code builds the full width dynamically.
using System;
namespace MyApp
{
class Program
{
static void Main(string[] args)
{
Console.Write("Enter the top letter (like E): ");
char top = Convert.ToChar(Console.ReadLine());
int n = top - 'A';
char[] alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray();
for (int i = 0; i <= n; i++)
{
for (int j = 0; j <= n; j++)
Console.Write(j <= i ? alpha[j] : ' ');
for (int k = n; k >= 0; k--)
Console.Write(k > i ? ' ' : alpha[k]);
Console.WriteLine();
}
}
}
}💡 Tips for Enhancement
Try These
- Replace the middle spaces with a symbol (see Program 15)
- Use lowercase by switching to
'a'as the base - Center the whole line by adding leading spaces
- Make the gap more visible by printing
.instead of spaces while testing
Avoid
- Using different widths for left and right halves (mirroring breaks)
- Printing tabs instead of spaces (alignment changes by editor)
- Letting top exceed
Zwithout handling it
Key Takeaways
Two fixed-width passes build left and right halves.
j <= i prints letters on the left.
k > i prints spaces on the right until the mirror starts.
The gap shrinks to zero on the last row.
❓ Frequently Asked Questions
12 people found this page helpful
