Convert a String into a square matrix grid of characters
Last Updated :
18 Sep, 2022
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:
- Without using an inbuilt function
- Make a 2d character array of (rows * column) size.
- Assign the value K which is a column value.
- Print the 2d character array.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
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];
for ( int i = 0; i < row; i++) {
for ( int j = 0; j < column; j++) {
s[i][j] = str[k];
k++;
}
}
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;
}
}
int main()
{
string str = "GEEKSFORGEEKS" ;
gridStr(str);
return 0;
}
|
Java
class GFG
{
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];
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++;
}
}
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( "" );
}
}
public static void main(String[] args)
{
String str = "GEEKSFORGEEKS" ;
gridStr(str);
}
}
|
Python3
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#
using System;
class GFG
{
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];
for ( int i = 0; i < row; i++)
{
for ( int j = 0; j < column; j++)
{
if (k < str.Length)
s[i,j] = str[k];
k++;
}
}
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( "" );
}
}
public static void Main()
{
String str = "GEEKSFORGEEKS" ;
gridStr(str);
}
}
|
PHP
<?php
function gridStr( $str )
{
$l = strlen ( $str );
$k = 0;
$row = floor (sqrt( $l ));
$column = ceil (sqrt( $l ));
if ( $row * $column < $l )
$row = $column ;
$s = array_fill (0, $row ,
array_fill (0, $column , "" ));
for ( $i = 0; $i < $row ; $i ++)
{
for ( $j = 0; $j < $column ; $j ++)
{
if (! empty ( $str [ $k ]))
$s [ $i ][ $j ] = $str [ $k ];
$k ++;
}
}
for ( $i = 0; $i < $row ; $i ++)
{
for ( $j = 0; $j < $column ; $j ++)
{
if ( $s [ $i ][ $j ] == '\0' )
break ;
echo $s [ $i ][ $j ];
}
echo "\n" ;
}
}
$str = "GEEKSFORGEEKS" ;
gridStr( $str );
?>
|
Javascript
<script>
function gridStr(str)
{
let l = str.length;
let k = 0, row, column;
row = Math.floor(Math.sqrt(l));
column = Math.ceil(Math.sqrt(l));
if (row * column < l)
{
row = column;
}
let s = new Array(row);
for (let i = 0; i < row; i++)
{
s[i] = new Array(column);
for (let j = 0; j < column; j++)
{
s[i][j] = 0;
}
}
for (let i = 0; i < row; i++)
{
for (let j = 0; j < column; j++)
{
if (k < str.length)
s[i][j] = str[k];
k++;
}
}
for (let i = 0; i < row; i++)
{
for (let j = 0; j < column; j++)
{
if (s[i][j] == 0)
{
break ;
}
document.write(s[i][j]);
}
document.write( "</br>" );
}
}
let str = "GEEKSFORGEEKS" ;
gridStr(str);
</script>
|
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)
Share your thoughts in the comments
Please Login to comment...