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

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.






