New: 213 pattern programs with tested code in C, C++, Java and Python Explore patterns

Alphabet Bridge Pattern (ABCDEDCBA, ABCD DCBA)

The alphabet bridge pattern, also called the reverse character bridge, starts with the palindrome ABCDEDCBA and opens a gap in the middle that grows by two spaces each row, until only the two A pillars are left.

Quick answer: Each row is 2n βˆ’ 1 characters wide. For each column work out k, the letter number counted from the nearer end of the row: j on the left half and 2n βˆ’ j on the right. Print the letter when k ≀ n βˆ’ i + 1 and a space otherwise.

This program is part of the String Patterns collection.

Table of contents8 sections
  1. Alphabet Bridge Pattern output
  2. How the alphabet bridge pattern works
  3. Alphabet Bridge Pattern in C
  4. Alphabet Bridge Pattern in C++
  5. Alphabet Bridge Pattern in Java
  6. Alphabet Bridge Pattern in Python
  7. Time and space complexity
  8. Frequently asked questions

Alphabet Bridge Pattern output

Output (n = 5):

ABCDEDCBA
ABCD DCBA
ABC   CBA
AB     BA
A       A
Alphabet Bridge Pattern layout diagram, row by row (n = 5)
How each row of the alphabet bridge pattern is built (n = 5). Blue cells are printed characters; grey dots are spaces.

How the alphabet bridge pattern works

Looking at the row from both ends at once makes the code short. Column 1 and column 2n βˆ’ 1 are both A, columns 2 and 2n βˆ’ 2 are both B, and so on, so k is the letter for both halves. Each row hides the letters above a limit, n βˆ’ i + 1, which starts at E and falls by one per row. Hiding the highest letter from both halves at once is what opens the gap two spaces at a time.

  1. Run the outer loop for i from 1 to n.
  2. For each column j from 1 to 2n βˆ’ 1, set k = j if j ≀ n, otherwise k = 2n βˆ’ j.
  3. Print letter k ('A' + k βˆ’ 1) if k ≀ n βˆ’ i + 1, otherwise a space.
  4. Move to the next line.

Key takeaways

  • Columns the same distance from either end hold the same letter.
  • Each row hides every letter above n βˆ’ i + 1.
  • The gap opens by two spaces per row: one letter disappears from each half.
  • The first row is the only one where E appears, and it appears once.

Alphabet Bridge Pattern in C

#include <stdio.h>

int main(void) {
    int n = 5;

    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= 2 * n - 1; j++) {
            int k = (j <= n) ? j : 2 * n - j;      /* letter number, counted from the nearer end */
            printf("%c", k <= n - i + 1 ? 'A' + k - 1 : ' ');
        }
        printf("\n");
    }
    return 0;
}

Alphabet Bridge Pattern in C++

#include <iostream>
using namespace std;

int main() {
    int n = 5;

    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= 2 * n - 1; j++) {
            int k = (j <= n) ? j : 2 * n - j;      // letter number, counted from the nearer end
            cout << (k <= n - i + 1 ? char('A' + k - 1) : ' ');
        }
        cout << endl;
    }
    return 0;
}

Alphabet Bridge Pattern in Java

public class AlphabetBridge {
    public static void main(String[] args) {
        int n = 5;

        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= 2 * n - 1; j++) {
                int k = (j <= n) ? j : 2 * n - j;      // letter number, counted from the nearer end
                System.out.print(k <= n - i + 1 ? (char) ('A' + k - 1) : ' ');
            }
            System.out.println();
        }
    }
}

Alphabet Bridge Pattern in Python

n = 5

for i in range(1, n + 1):
    for j in range(1, 2 * n):
        k = j if j <= n else 2 * n - j          # letter number, counted from the nearer end
        print(chr(64 + k) if k <= n - i + 1 else " ", end="")
    print()

Time and space complexity

The program prints n(2n βˆ’ 1) characters, so it runs in O(nΒ²) time with O(1) extra memory.

Frequently asked questions

How do I make the gap start on the first row?

Use k ≀ n βˆ’ i as the test and print n letters on each side. The first row then reads ABCDE EDCBA, with the E doubled around a one-space gap.

Is this the same as the number bridge?

It is the same shape upside down and with letters. The number bridge starts with a wide gap and closes it; this bridge starts closed and opens.

Can I print it with numbers?

Yes. Print k instead of the letter: 123454321, then 1234 4321, and so on.

This tutorial was drafted with AI assistance. Spotted a mistake? Tell us and we fix it. Read our editorial policy.

Quick check: can you predict the output?

Answer from the code, then check. Every answer comes from the real output above.

Pattern 17 of 22 in String Patterns

 

Still stuck? Ask AI to explain this code

Opens your favourite AI with this page, ready to explain every loop step by step.

Authored by

Ghanendra Yadav runs Pattern Programs from Bangalore, India. The site started in 2020 to help students practise loops and logic through pattern programs in C, C++, Java and Python. Every program published here is compiled and run before it goes live, and the output shown is the real output.

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.