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

String Plus Pattern (Word in a Plus Shape)

The string plus pattern writes a word twice inside a square: once across the middle row and once down the middle column, so the two copies cross at the centre letter. All other cells are filled with X.

Quick answer: For a word of odd length n, set mid = n / 2. In an n × n grid, print s[j] on the middle row, s[i] on the middle column, and X everywhere else. With FEVER the centre cell is V on both lines.

This program is part of the String Patterns collection.

Table of contents8 sections
  1. String Plus Pattern output
  2. How the string plus pattern works
  3. String Plus Pattern in C
  4. String Plus Pattern in C++
  5. String Plus Pattern in Java
  6. String Plus Pattern in Python
  7. Time and space complexity
  8. Frequently asked questions

String Plus Pattern output

Output (word “FEVER”):

X X F X X
X X E X X
F E V E R
X X E X X
X X R X X
String Plus Pattern layout diagram, row by row (word "FEVER")
How each row of the string plus pattern is built (word “FEVER”). Blue cells are printed characters; grey dots are spaces.

Same shape, other characters: the Number Frame Layers Pattern, Number and Star Checker Grid and Square Star Pattern programs draw this same outline with different characters. Putting them side by side shows which part of the loop decides the shape and which part decides what is printed.

How the string plus pattern works

The middle row reads the word left to right, so the letter comes from the column index j. The middle column reads it top to bottom, so the letter comes from the row index i. Testing the row first means the centre cell is printed once, as part of the row. An odd length is needed so that there is a single middle row and column, and the centre letter is the same in both copies.

Key takeaways

  • The middle row uses the column index; the middle column uses the row index.
  • An odd-length word gives one centre cell shared by both copies.
  • The filler, X here, can be any character.
  • It uses the same middle-row-and-column idea as the plus sign pattern.

String Plus Pattern in C

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

int main(void) {
    char s[] = "FEVER";
    int n = strlen(s);                          /* use a word with an odd length */
    int mid = n / 2;

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

String Plus Pattern in C++

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

int main() {
    string s = "FEVER";
    int n = s.length();                         // use a word with an odd length
    int mid = n / 2;

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

String Plus Pattern in Java

public class StringPlus {
    public static void main(String[] args) {
        String s = "FEVER";
        int n = s.length();                         // use a word with an odd length
        int mid = n / 2;

        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                if (i == mid)
                    System.out.print(s.charAt(j) + " ");
                else if (j == mid)
                    System.out.print(s.charAt(i) + " ");
                else
                    System.out.print("X ");
            }
            System.out.println();
        }
    }
}

String Plus Pattern in Python

s = "FEVER"
n = len(s)                                  # use a word with an odd length
mid = n // 2

for i in range(n):
    for j in range(n):
        if i == mid:
            print(s[j], end=" ")
        elif j == mid:
            print(s[i], end=" ")
        else:
            print("X", end=" ")
    print()

Time and space complexity

The program prints n² characters for a word of length n, so it runs in O(n²) time with O(1) extra memory.

Frequently asked questions

What if the word has an even length?

There is no single middle row or column, so the two copies cannot cross on one letter. Pad the word to an odd length or choose a different word.

How do I print the word along the diagonals instead?

Use the diagonal tests j == i and j == n − i − 1, as in the word in X pattern.

Why are the indexes 0-based here?

Strings are indexed from 0 in C, C++, Java and Python, so it is simpler to run the loops from 0 to n − 1 and use the indexes directly.

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