Printing string in plus ‘+’ pattern in the matrix
Last Updated :
17 Feb, 2023
Given a string, print it inside a matrix in such a way that a 'plus' is formed.
Examples:
Input: TOP
Output:
X T X
T O P
X P X
Input: FEVER
Output:
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
Approach:
The idea is simple. First we can access every element of the matrix and make it ‘X’. Then we will insert the characters of the string in the middle row as well as in the middle column of the matrix. For example, we have string of length 5. So we will need a (5X5) matrix for it.

To access the middle column of the matrix, column index is made constant and is equal to (n/2), where n is the length of the string. Row index will go from 0 to (n-1) for it.
To access the middle row, the row index will be made constant and equal to (n/2) and the column index will go from 0 to (n-1).
Below is the implementation of above approach:
C++
// CPP program to print the
// string in 'plus' pattern
#include <bits/stdc++.h>
#define max 100
using namespace std;
// Function to make a cross in the matrix
void carveCross(string str)
{
int n = str.length();
if (n % 2 == 0)
{
/* As, it is not possible to make
the cross exactly in the middle of
the matrix with an even length string.*/
cout << "Not possible. Please enter "
<< "odd length string.\n";
}
else {
// declaring a 2D array i.e a matrix
char arr[max][max];
int m = n / 2;
/* Now, we will fill all the
elements of the array with 'X'*/
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
arr[i][j] = 'X';
}
}
/* Now, we will place the characters
of the string in the matrix, such
that a cross is formed in it.*/
for (int i = 0; i < n; i++)
{
/* here the characters of the
string will be added in the
middle column of our array.*/
arr[i][m] = str[i];
}
for (int i = 0; i < n; i++)
{
// here the characters of the
// string will be added in the
// middle row of our array.
arr[m][i] = str[i];
}
/* Now finally, we will print
the array*/
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cout << arr[i][j] << " ";
}
cout << "\n";
}
}
}
// driver code
int main()
{
string str = "PICTURE";
carveCross(str);
return 0;
}
Java
// Java program to print the
// string in 'plus' pattern
class GFG {
static final int max = 100;
// Function to make a cross in the matrix
static void carveCross(String str) {
int n = str.length();
if (n % 2 == 0) {
// As, it is not possible to make
// the cross exactly in the middle of
// the matrix with an even length string.
System.out.print("Not possible. Please enter "
+ "odd length string.\n");
}
else {
// declaring a 2D array i.e a matrix
char arr[][] = new char[max][max];
int m = n / 2;
// Now, we will fill all the
// elements of the array with 'X'
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
arr[i][j] = 'X';
}
}
// Now, we will place the characters
// of the string in the matrix, such
// that a cross is formed in it.
for (int i = 0; i < n; i++) {
// here the characters of the
// string will be added in the
// middle column of our array.
arr[i][m] = str.charAt(i);
}
for (int i = 0; i < n; i++) {
// here the characters of the
// string will be added in the
// middle row of our array.
arr[m][i] = str.charAt(i);
}
// Now finally, we will print
// the array
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
System.out.print(arr[i][j] + " ");
}
System.out.print("\n");
}
}
}
// Driver code
public static void main(String[] args) {
String str = "PICTURE";
carveCross(str);
}
}
// This code is contributed by Anant Agarwal.
Python 3
# Python 3 program to print the
# string in 'plus' pattern
max = 100
# Function to make a cross
# in the matrix
def carveCross(str):
n = len(str)
if (n % 2 == 0) :
''' As, it is not possible to make
the cross exactly in the middle of
the matrix with an even length string.'''
print("Not possible. Please enter "
, "odd length string.\n")
else :
# declaring a 2D array i.e a matrix
arr = [[ False for x in range(max)]
for y in range(max)]
m = n // 2
''' Now, we will fill all the
elements of the array with 'X'''
for i in range( n) :
for j in range(n) :
arr[i][j] = 'X'
'''Now, we will place the characters
of the string in the matrix, such
that a cross is formed in it.'''
for i in range(n):
''' here the characters of the
string will be added in the
middle column of our array.'''
arr[i][m] = str[i]
for i in range(n):
# here the characters of the
# string will be added in the
# middle row of our array.
arr[m][i] = str[i]
# Now finally, we will print
# the array
for i in range(n):
for j in range(n):
print( arr[i][j] , end=" ")
print()
# Driver Code
if __name__ == "__main__":
str = "PICTURE"
carveCross(str)
# This code is contributed
# by ChitraNayal
C#
// C# program to print the
// string in 'plus' pattern
using System;
class GFG {
static int max = 100;
// Function to make a cross in the matrix
static void carveCross(String str) {
int n = str.Length;
if (n % 2 == 0) {
// As, it is not possible to make
// the cross exactly in the middle of
// the matrix with an even length string.
Console.Write("Not possible. Please enter "
+ "odd length string.");
}
else {
// declaring a 2D array i.e a matrix
char [,]arr = new char[max,max];
int m = n / 2;
// Now, we will fill all the
// elements of the array with 'X'
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
arr[i,j] = 'X';
}
}
// Now, we will place the characters
// of the string in the matrix, such
// that a cross is formed in it.
for (int i = 0; i < n; i++) {
// here the characters of the
// string will be added in the
// middle column of our array.
arr[i,m] = str[i];
}
for (int i = 0; i < n; i++) {
// here the characters of the
// string will be added in the
// middle row of our array.
arr[m,i] = str[i];
}
// Now finally, we will print
// the array
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
Console.Write(arr[i,j] + " ");
}
Console.WriteLine();
}
}
}
// Driver code
public static void Main() {
string str = "PICTURE";
carveCross(str);
}
}
// This code is contributed by vt_m.
PHP
<?php
// PHP program to print the string
// in 'plus' pattern
// Function to make a cross
// in the matrix
function carveCross($str)
{
$n = strlen($str);
if ($n % 2 == 0)
{
/* As, it is not possible to make
the cross exactly in the middle of
the matrix with an even length string.*/
echo ("Not possible. Please enter ");
echo( "odd length string.\n");
}
else
{
// declaring a 2D array i.e a matrix
$arr = array();
$m = $n / 2;
/* Now, we will fill all the
elements of the array with 'X'*/
for ($i = 0; $i < $n; $i++)
{
for ($j = 0; $j < $n; $j++)
{
$arr[$i][$j] = 'X';
}
}
/* Now, we will place the characters
of the string in the matrix, such
that a cross is formed in it.*/
for ($i = 0; $i < $n; $i++)
{
/* here the characters of the
string will be added in the
middle column of our array.*/
$arr[$i][$m] = $str[$i];
}
for ($i = 0; $i < $n; $i++)
{
// here the characters of the
// string will be added in the
// middle row of our array.
$arr[$m][$i] = $str[$i];
}
/* Now finally, we will print
the array*/
for ($i = 0; $i < $n; $i++)
{
for ($j = 0; $j < $n; $j++)
{
echo ($arr[$i][$j] . " " );
}
echo ("\n");
}
}
}
// Driver Code
$str = "PICTURE";
carveCross($str);
// This code is contributed
// by Shivi_Aggarwal
?>
JavaScript
<script>
// JavaScript program to print the
// string in 'plus' pattern
const max = 100
// Function to make a cross in the matrix
function carveCross(str)
{
let n = str.length;
if (n % 2 == 0)
{
/* As, it is not possible to make
the cross exactly in the middle of
the matrix with an even length string.*/
document.write("Not possible. Please enter odd length string.","</br>");
}
else {
// declaring a 2D array i.e a matrix
let arr = new Array(max);
for(let i=0;i<max;i++){
arr[i] = new Array(max);
}
let m = Math.floor(n / 2);
/* Now, we will fill all the
elements of the array with 'X'*/
for (let i = 0; i < n; i++) {
for (let j = 0; j < n; j++) {
arr[i][j] = 'X';
}
}
/* Now, we will place the characters
of the string in the matrix, such
that a cross is formed in it.*/
for (let i = 0; i < n; i++)
{
/* here the characters of the
string will be added in the
middle column of our array.*/
arr[i][m] = str[i];
}
for(let i = 0; i < n; i++)
{
// here the characters of the
// string will be added in the
// middle row of our array.
arr[m][i] = str[i];
}
/* Now finally, we will print
the array*/
for (let i = 0; i < n; i++) {
for (let j = 0; j < n; j++) {
document.write(arr[i][j] + " ");
}
document.write("</br>");
}
}
}
// driver code
let str = "PICTURE";
carveCross(str);
// This code is contributed by shinjanpatra
</script>
Output:
X X X P X X X
X X X I X X X
X X X C X X X
P I C T U R E
X X X U X X X
X X X R X X X
X X X E X X X
Time Complexity : O(n2)
Auxiliary Space: O(MAX2) where MAX is a defined constant
Similar Reads
Print matrix in snake pattern Given an n x n matrix. In the given matrix, you have to print the elements of the matrix in the snake pattern.Examples : Input: mat[][] = { {10, 20, 30, 40}, {15, 25, 35, 45}, {27, 29, 37, 48}, {32, 33, 39, 50}};Output: 10 20 30 40 45 35 25 15 27 29 37 48 50 39 33 32 Input: mat[][] = [[1, 2], [3, 4]
4 min read
Print matrix in diagonal pattern Given a matrix of n*n size, the task is to print its elements in a diagonal pattern. Input : mat[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}} Output : 1 2 4 7 5 3 6 8 9. Explanation: Start from 1 Then from upward to downward diagonally i.e. 2 and 4 Then from downward to upward diagonally i.e 7, 5, 3 Th
14 min read
Print matrix in snake pattern from the last column Given a matrix of 2-Dimensional array of n rows and n columns. Print this matrix in snake fashion starting from column n-1 as shown in the figure below . Examples: Input : mat[][] = 1 2 3 4 5 6 7 8 9 Output: 3 2 1 4 5 6 9 8 7 Input: mat[][] = 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 Output: 4 3 2 1 5
6 min read
POTD Solutions | 8 Novâ 23 | Print Matrix in snake Pattern View all POTD Solutions Welcome to the daily solutions of our PROBLEM OF THE DAY (POTD). We will discuss the entire problem step-by-step and work towards developing an optimized solution. This will not only help you brush up on your concepts of Matrix but will also help you build up problem-solving
4 min read
Javascript Program to Print matrix in snake pattern Given n x n matrix In the given matrix, you have to print the elements of the matrix in the snake pattern.Examples: Input :mat[][] = { {10, 20, 30, 40}, {15, 25, 35, 45}, {27, 29, 37, 48}, {32, 33, 39, 50}}; Output : 10 20 30 40 45 35 25 15 27 29 37 48 50 39 33 32 Input :mat[][] = { {1, 2, 3}, {4, 5
2 min read
Print numbers in matrix diagonal pattern Given an integer N, the task is to print the given pattern. Examples: Input: 3 Output: 1 2 4 3 5 7 6 8 9 Input: 4 Output: 1 2 4 7 3 5 8 11 6 9 12 14 10 13 15 16 Approach: Create a matrix of size N X N which will store the pattern before printing.Store the elements in the upper triangle of the patter
7 min read
Print lower triangular matrix pattern from given array Given an array, print the array in 2D form where upper triangle has values 0 and lower triangle has values increasing prefix sizes (First row has prefix of size 1, second row has prefix of size 2, ..)Examples : Input : 1 2 3 4 5 Output : 1 0 0 0 0 1 2 0 0 0 1 2 3 0 0 1 2 3 4 0 1 2 3 4 5 Input : 1 2
6 min read
Print concentric rectangular pattern in a 2d matrix Given a positive integer n, print the matrix filled with rectangle pattern as shown below: a a a a a a b b b a a b c b a a b b b a a a a a a where a = n, b = n - 1,c = n - 2 and so on. Examples: Input : n = 4 Output : 4 4 4 4 4 4 4 4 3 3 3 3 3 4 4 3 2 2 2 3 4 4 3 2 1 2 3 4 4 3 2 2 2 3 4 4 3 3 3 3 3
13 min read
Program to print numeric pattern | Set - 2 Given a number as 'num', and a number of lines as 'num_of_lines' where 'num' implies the starting number from which the pattern has to be started and 'num_of_lines' implies the number of lines that have to be printed. Now, according to the above information, print a pattern as given below. Examples:
5 min read
Pattern Printing question asked in CGI Coding Round Write a program that receives a number as input and prints it in the following format as shown below. Examples : Input : n = 3 Output : 1*2*3*10*11*12 --4*5*8*9 ----6*7 Input : n = 4 Output : 1*2*3*4*17*18*19*20 --5*6*7*14*15*16 ----8*9*12*13 ------10*11 Asked in CGI coding round Approach: The appro
15+ min read