Printing string in plus ‘+’ pattern in the matrix
Given a string, print it inside a matrix in such a way that a ‘plus’ is formed.
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
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:
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
Attention reader! Don’t stop learning now. Join the First-Step-to-DSA Course for Class 9 to 12 students , specifically designed to introduce data structures and algorithms to the class 9 to 12 students