Home > other >  How can I populate jagged array with chars in c#
How can I populate jagged array with chars in c#

Time:03-23

I am struggling to populate jagged array with chars My goal is to read string. slice them into chars, then populate jagged array with those chars

            "Lorem",
            5,
            new char[][]
            {
                new char[] { 'L', 'o', 'r', 'e', 'm' },
            },

or

            "Loremipsumdolorsitamet",
            5,
            new char[][]
            {
                new char[] { 'L', 'o', 'r', 'e', 'm' },
                new char[] { 'i', 'p', 's', 'u', 'm' },
                new char[] { 'd', 'o', 'l', 'o', 'r' },
                new char[] { 's', 'i', 't', 'a', 'm' },
                new char[] { 'e', 't' },
            },

like the example. above ints under the string represent arraySize(i.e. number of columns) and streamReader(see code under) is the string itself. I am using System.IO and System.Text

        var read = streamReader.ReadToEnd().ToCharArray();
        if (read.Length == 0)
        {
            return Array.Empty<char[]>();
        }

        char[][] jagArray = new char[read.Length / 5][];

        for (int p = 0; p < read.Length; p  )
        {
            for (int i = 0; i < read.Length / arraySize; i  )
            {
                jagArray[i] = new char[arraySize];

                for (int j = 0; j < arraySize; j  )
                {
                    jagArray[i][j] = read[p];
                }
            }
        }

        return jagArray;

I tried this code, but obviously it doesnt work.

CodePudding user response:

Use this method to split a single char[] array into array of arrays, each of which is no longer than cols characters length:

public static char[][] ToJaggedArray(char[] chars, int cols)
{
    int rows = (chars.Length   cols - 1) / cols;
    char[][] jagArray = new char[rows][];
    for (int i = 0; i < rows; i  )
    {
        jagArray[i] = new char[Math.Min(cols, chars.Length - cols * i)];
    }

    for (int i = 0; i < chars.Length; i  )
    {
        int row = i / cols;
        int col = i % cols;
        jagArray[row][col] = chars[i];
    }

    return jagArray;
}

CodePudding user response:

string str = "Loremipsumdolorsitamet";
int slice = 5;
List<List<char>> list = new List<List<char>>();
List<char> list2 = new List<char>();
int limit = str.Length/5  1;
for (int i = 0; i < limit; i  )
{
    if (str.Length <= 5)
    {
        slice = str.Length;
    }
    for (int j = 0; j < slice; j  )
    {
        list2.Add(str[j]);
    }
    str = str.Remove(0, slice);
    list.Add(list2);
    list2 = new List<char>();
}

Hopefully I understood you correctly, This is my very scuffed way of doing it, hopefully some of the more experienced users can provide some info to improve this.

EDIT. OUTPUT:

foreach(var a in list)
{
    foreach (var b in a)
    {
        Console.Write(b   " ");
    }
    Console.WriteLine();
}

L o r e m
i p s u m
d o l o r
s i t a m
e t
  • Related