C#C
C#3y ago
malkav

✅ Reorder letters in a string and group per...

So I've been puzzling with this for a while now, and I am not sure if this actually is what I am looking for but before I dive into what I have, let me explain the premise:

Say I have a text (say a lorem of 200 words) and I strip all punctuations and spaces and newlines and whatever to turn the entire lorem into a single string without any spaces/punctuation-marks/newlines (\r\n)

Now I group all of these by 6 characters into a single array (done)
Here's the tricky part that I am puzzling with:

I have to turn the whole string into something that looks like this:
lor psu lor ame nse
emi mdo sit tco cte

tur
--- (etc)


So basically, group by 6 characters, and write 5 of those groups in a single "line" (which is obviously 2 lines)
But actually I have to reorder these letters before I do so.

Now the reordering is easy, and the grouping by 6 is easy. It's the actual "how do I write them on two lines with the indexes 0, 1, and 2 on the first line, and 3, 4, 5 on the second line.

Here's what I have to begin with:

private static IEnumerable<string> WriteByLine(this string[] groups, string order, int startIndex = 0)
{
    string text = "";
    int groupIndex = 0;

    foreach (string item in groups)
    {
        // TODO: Change this next line to the actual order of items instead of just the first or second three items
        text += item.Reorder(order).Substring(startIndex, Math.Min(3, item.Length));
        if (groupIndex == 4)
        {
            text += "\n";
            groupIndex = 0;
        }
        else
        {
            text += " ";
        }

        groupIndex++;
    }

    return text.Split("\n");
}


but I fear it's not entirely doing what I wanted... please help me out?
Was this page helpful?