Convierta una string en una cuadrícula de array cuadrada de caracteres

Dada una string de longitud L. La tarea es convertir la string en una cuadrícula.
Ejemplos: 
 

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

Nota: & l = longitud de la string 
 

Acercarse: 
 

  1. Sin usar una función incorporada
  2. Cree una array de caracteres 2d de tamaño (filas * columna).
  3. Asigne el valor K que es un valor de columna.
  4. Imprima la array de caracteres 2d.

A continuación se muestra la implementación del enfoque anterior. 
 

C++

#include <bits/stdc++.h>
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

<?php
// PHP implementation of the
// above approach
 
// Function to string into grid form
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, ""));
    // convert the string into grid
    for ($i = 0; $i < $row; $i++)
    {
        for ($j = 0; $j < $column; $j++)
        {
            if(!empty($str[$k]))
            $s[$i][$j] = $str[$k];
            $k++;
        }
    }
 
    // Printing the grid
    for ($i = 0; $i < $row; $i++)
    {
        for ($j = 0; $j < $column; $j++)
        {
            if ($s[$i][$j] == '\0')
                break;
            echo $s[$i][$j];
        }
        echo "\n";
    }
}
 
// Driver code
$str = "GEEKSFORGEEKS";
gridStr($str);
 
// This code is contributed by mits
?>

Javascript

<script>
    // Javascript implementation of the above approach
     
    // Function to string into grid form
    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;
            }
        }
           
        // convert the string into grid
        for (let i = 0; i < row; i++)
        {
            for (let j = 0; j < column; j++)
            {
                if(k < str.length)
                    s[i][j] = str[k];
                k++;
            }
        }
   
        // Printing the grid
        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);
 
// This code is contributed by decode2207.
</script>
Producción: 

GEEK
SFOR
GEEK
S

 

Publicación traducida automáticamente

Artículo escrito por SURENDRA_GANGWAR y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *