# Convert a String into a square matrix grid of characters

Given a string of length L. The task is to convert the string into a grid.
Examples:

```Input : str = "haveaniceday"
Output :  have
anic
eday

Explanation: k is the separator. If k is 4 then the output will be "have
anic
eday"

Input :str = "geeksforgeeks"
Output : geek
sfor
geek
s```

Note: & l = length of the string

Approach:

1. Without using an inbuilt function
2. Make a 2d character array of (rows * column) size.
3. Assign the value K which is a column value.
4. Print the 2d character array.

Below is the implementation of the above approach.

## C++

 `#include ``using` `namespace` `std;` `// Function to string into grid form``void` `gridStr(string str)``{``    ``int` `l = str.length();``    ``int` `k = 0, row, column;``    ``row = ``floor``(``sqrt``(l));``    ``column = ``ceil``(``sqrt``(l));` `    ``if` `(row * column < l)``        ``row = column;` `    ``char` `s[row][column];``    ``// convert the string into grid``    ``for` `(``int` `i = 0; i < row; i++) {``        ``for` `(``int` `j = 0; j < column; j++) {``            ``s[i][j] = str[k];``            ``k++;``        ``}``    ``}` `    ``// Printing the grid``    ``for` `(``int` `i = 0; i < row; i++) {``        ``for` `(``int` `j = 0; j < column; j++) {``            ``if` `(s[i][j] == ``'\0'``)``                ``break``;``            ``cout << s[i][j];``        ``}``        ``cout << endl;``    ``}``}` `// Driver code``int` `main()``{``    ``string str = ``"GEEKSFORGEEKS"``;``    ``gridStr(str);``    ` `    ``return` `0;``}`

## Java

 `// Java implementation of the``// above approach``class` `GFG``{` `    ``// Function to string into grid form``    ``static` `void` `gridStr(String str)``    ``{``        ``int` `l = str.length();``        ``int` `k = ``0``, row, column;``        ``row = (``int``) Math.floor(Math.sqrt(l));``        ``column = (``int``) Math.ceil(Math.sqrt(l));` `        ``if` `(row * column < l)``        ``{``            ``row = column;``        ``}` `        ``char` `s[][] = ``new` `char``[row][column];``        ` `        ``// convert the string into grid``        ``for` `(``int` `i = ``0``; i < row; i++) ``        ``{``            ``for` `(``int` `j = ``0``; j < column; j++) ``            ``{``                ``if``(k < str.length())``                    ``s[i][j] = str.charAt(k);``                ``k++;``            ``}``        ``}` `        ``// Printing the grid``        ``for` `(``int` `i = ``0``; i < row; i++) ``        ``{``            ``for` `(``int` `j = ``0``; j < column; j++)``            ``{``                ``if` `(s[i][j] == ``0``) ``                ``{``                    ``break``;``                ``}``                ``System.out.print(s[i][j]);``            ``}``            ``System.out.println(``""``);``        ``}``    ``}` `    ``// Driver code``    ``public` `static` `void` `main(String[] args)``    ``{``        ``String str = ``"GEEKSFORGEEKS"``;``        ``gridStr(str);``    ``}``}` `//This code is contributed by Rajput-Ji`

## Python3

 `# Python3 implementation of the``# above approach`   `# Function to string into grid form ``def` `function(``str``, k):` `    ``for` `i ``in` `range``(``len``(``str``)):``        ``if` `i ``%``k ``=``=` `0``:``            ``sub ``=` `str``[i:i``+``k]``            ``lst ``=` `[]``            ``for` `j ``in` `sub:``                ``lst.append(j)``            ``print``(``' '``.join(lst))` `function(``"GEEKSFORGEEKS"``, ``5``)` `/``*` `This code contributed by nsew1999gokulcvan ``*``/`

## C#

 `// C# implementation of the``// above approach``using` `System;` `class` `GFG``{` `    ``// Function to string into grid form``    ``static` `void` `gridStr(String str)``    ``{``        ``int` `l = str.Length;``        ``int` `k = 0, row, column;``        ``row = (``int``) Math.Floor(Math.Sqrt(l));``        ``column = (``int``) Math.Ceiling(Math.Sqrt(l));` `        ``if` `(row * column < l)``        ``{``            ``row = column;``        ``}` `        ``char` `[,]s = ``new` `char``[row,column];``        ` `        ``// convert the string into grid``        ``for` `(``int` `i = 0; i < row; i++) ``        ``{``            ``for` `(``int` `j = 0; j < column; j++) ``            ``{``                ``if``(k < str.Length)``                    ``s[i,j] = str[k];``                ``k++;``            ``}``        ``}` `        ``// Printing the grid``        ``for` `(``int` `i = 0; i < row; i++) ``        ``{``            ``for` `(``int` `j = 0; j < column; j++)``            ``{``                ``if` `(s[i, j] == 0) ``                ``{``                    ``break``;``                ``}``                ``Console.Write(s[i, j]);``            ``}``            ``Console.WriteLine(``""``);``        ``}``    ``}` `    ``// Driver code``    ``public` `static` `void` `Main()``    ``{``        ``String str = ``"GEEKSFORGEEKS"``;``        ``gridStr(str);``    ``}``}` `/* This code contributed by PrinciRaj1992 */`

## PHP

 ``

## Javascript

 ``

Output:
```GEEK
SFOR
GEEK
S```

Time complexity: O(row*column), where L is length of given string and row = floor(sqrt(L)), column = ceil(sqrt(L))

Auxiliary space: O(row*column)

Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!

Previous
Next