Click here to Skip to main content
15,891,607 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,

I am working a string consisting of 2 lines with characters 1 and 0.
I have placed the string into a string array to extract the lines.

Ideally, I would like to loop through this array and determine if the current element is 1 or 0. if it is a 1 I would like to place a '#' character into another array of type char. if it is a 0 I would like to place a 'X' so when i loop this new array i get a similar array but with the new characters.

For some reason, my char array doesn't print to screen, Could someone please point me in the right direction?

What I have tried:

class MainClass
    {
        const string maze = @"
            1 1 1 1 1
            1 0 0 0 1";
        
        public static void Main(string[] args)
        {
            //Display(GetMazeArray(maze));
            string[] lines = maze.Split(new char[] { '\n' },
                StringSplitOptions.RemoveEmptyEntries);

            char[][] characters = new char[2][];
            characters[0] = lines[0].ToCharArray();
            characters[1] = lines[1].ToCharArray();

            // new array to store the new characters
            char[][] newArr = new char[2][];

            for (int i = 0; i < 2; i++) 
            {
                for (int j = 0; j < 4; j++)
                {
                    if(characters[i][j] == '1')
                    {
                        newArr[i][j] = '#';
                    }
                    else if(characters[i][j] == '0')
                    {
                        newArr[i][j] = 'X';
                    }
                }
            }

            for (int i = 0; i < 2; i++)
            {
                for (int j = 0; j < 4; j++)
                {
                    Console.WriteLine(newArr[i][j]);
                }
            }
        }
}
Posted
Updated 8-May-18 10:46am

There are three problems with your current code:

  • newArr[0] and newArr[1] are still null because you didn't assign empty arrays to these values yet. Do this:
    C#
    // new array to store the new characters
    char[][] newArr = new char[2][];
    newArr[0] = new char[characters[0].Length];
    newArr[1] = new char[characters[1].Length];
    

  • .ToCharArray() will also give you a bunch of spaces in your characters array, which you don't want because you only are about 0 and 1. Remove the spaces first before using ToCharArray:
    C#
    char[][] characters = new char[2][];
    characters[0] = lines[0].Replace(" ", "").ToCharArray();
    characters[1] = lines[1].Replace(" ", "").ToCharArray();
    

  • You use j < 4 in your for loops; but your characters[i] arrays have 5 elements (the last index is 4) so you want j <= 4 or j < 5.

All these problems resolved:
C#
using System;

class MainClass
    {
        const string maze = @"
            1 1 1 1 1
            1 0 0 0 1";
        
        public static void Main(string[] args)
        {
            //Display(GetMazeArray(maze));
            string[] lines = maze.Split(new char[] { '\n' },
                StringSplitOptions.RemoveEmptyEntries);

            char[][] characters = new char[2][];
            characters[0] = lines[0].Replace(" ", "").ToCharArray();
            characters[1] = lines[1].Replace(" ", "").ToCharArray();

            // new array to store the new characters
            char[][] newArr = new char[2][];
            newArr[0] = new char[characters[0].Length];
            newArr[1] = new char[characters[1].Length];

            for (int i = 0; i < 2; i++) 
            {
                for (int j = 0; j < 5; j++)
                {
                    if(characters[i][j] == '1')
                    {
                        newArr[i][j] = '#';
                    }
                    else if(characters[i][j] == '0')
                    {
                        newArr[i][j] = 'X';
                    }
                }
            }

            for (int i = 0; i < 2; i++)
            {
                for (int j = 0; j < 5; j++)
                {
                    Console.WriteLine(newArr[i][j]);
                }
            }
        }
}
 
Share this answer
 
Comments
Maciej Los 8-May-18 16:11pm    
Detection of 3 problems deserves for 5!
BillWoodruff 8-May-18 16:46pm    
+5
BillWoodruff 8-May-18 16:47pm    
this can be simplified a bit: see code below
You can reduce the amount of 'String creation and array manipulation like this:

private char[] splitary = new char[] {'\n','\r'};

// in some method:

StringBuilder sb = new StringBuilder(maze);
sb.Replace(" ", String.Empty);
sb.Replace('0', 'X');
sb.Replace('1', '#');

var trimmed = sb.ToString().Split(splitary, StringSplitOptions.RemoveEmptyEntries);

char[][] ary = new char[][]
{
    (trimmed[0].ToCharArray()),
    (trimmed[1].ToCharArray())
};
But, keep in mind that using an automatic Array initializer means you cannot specify the 'Length.
 
Share this answer
 
Comments
Thomas Daniels 8-May-18 16:48pm    
5; I was very focused on the errors but indeed, this is a much eleganter way to solve the whole problem.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900