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

Word in X Pattern

The word in X pattern writes a word along both diagonals of a square, so the letters cross in the middle. With PROGRAM, the X reads P to M from top left to bottom right and M to P from top right to bottom left.

Quick answer: For a word of length n, loop over an n × n grid. Print the letter at position j of the word when the cell is on a diagonal, j == i or j == n − i + 1, and a space everywhere else. An odd-length word gives a single letter in the middle.

This program is part of the String Patterns collection.

Table of contents8 sections
  1. Output
  2. How the word in x pattern works
  3. Program in C
  4. Program in C++
  5. Program in Java
  6. Program in Python
  7. Time and space complexity
  8. Frequently asked questions

Word in X Pattern output

Output (word “PROGRAM”):

P     M
 R   A
  O R
   G
  O R
 R   A
P     M
Word in X Pattern layout diagram, row by row (word "PROGRAM")
How each row of the word in x pattern is built (word “PROGRAM”). Blue cells are printed characters; grey dots are spaces.

How the word in x pattern works

The two diagonals are the same ones used in the X (cross) pattern. The only change is what gets printed: instead of a star, each diagonal cell shows the character in its column. Because the letter comes from the column, both diagonals spell the word left to right on every row, which is why row 2 shows R on the left and A on the right.

  1. Run the outer loop for i from 1 to n, one row per pass.
  2. Go through n positions in the row. Print characters of the word where j = i or j = n − i + 1; print spaces everywhere else.
  3. Move to the next line.

Key takeaways

  • The X is the two diagonals: j == i and j == n − i + 1.
  • Each diagonal cell prints the word’s letter for its column.
  • An odd-length word, like PROGRAM, has one centre letter: G.
  • In C the word is a char array and strlen gives its length.

Word in X Pattern in C

#include <stdio.h>
#include <string.h>

int main(void) {
    char s[] = "PROGRAM";
    int n = strlen(s);

    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            if (j == i || j == n - i + 1)
                printf("%c", s[j - 1]);
            else
                printf(" ");
        }
        printf("\n");
    }
    return 0;
}

Word in X Pattern in C++

#include <iostream>
#include <string>
using namespace std;

int main() {
    string s = "PROGRAM";
    int n = s.length();

    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            if (j == i || j == n - i + 1)
                cout << s[j - 1];
            else
                cout << " ";
        }
        cout << endl;
    }
    return 0;
}

Word in X Pattern in Java

public class WordX {
    public static void main(String[] args) {
        String s = "PROGRAM";
        int n = s.length();

        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= n; j++) {
                if (j == i || j == n - i + 1)
                    System.out.print(s.charAt(j - 1));
                else
                    System.out.print(" ");
            }
            System.out.println();
        }
    }
}

Word in X Pattern in Python

s = "PROGRAM"
n = len(s)

for i in range(1, n + 1):
    for j in range(1, n + 1):
        if j == i or j == n - i + 1:
            print(s[j - 1], end="")
        else:
            print(" ", end="")
    print()

Time and space complexity

The program prints one character for each position in the pattern, so for n rows it runs in O(n²) time. It only needs a few loop variables, so the extra memory is O(1).

Frequently asked questions

What happens with an even-length word?

The diagonals never share a cell, so the two middle rows each show two neighbouring letters, for example with CODE: the middle rows read ” OD” and ” OD”. The X still forms, without a single centre.

How do I use my own word?

Change the string at the top. The length is worked out from it, so the grid resizes automatically. Read it from the keyboard with scanf("%s", s) in C or input() in Python.

Why does each row show different letters on the left and right?

Because the letter is chosen by the column. The left diagonal is in column i and the right one in column n − i + 1, so they hold different letters of the word.

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 11 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.