Can someone say what i do wrong in C# task?

Viewed 110

I need make symmetric letter “W” in string Array

get_w() Is method that should return string array that contains letter "W"

get_w(5) # should return:

public string[] GetW(int h)
        {
            if (h < 2) return new string[h];
            int row = 0;
            int stars_number = h * 4 - 3;
            int times = 0;
            StringBuilder[] c = new StringBuilder[h];
            for(int a = 0; a < h; a++)
            {
                c[a] = new StringBuilder();
                c[a].Length = stars_number;
            }
            for (int i = 0; i < stars_number; i++)
            {
                if (i == h - 1) times = 1;
                if (i == stars_number-2 * h + 1) times = 2;
                if (i == stars_number - h) times = 3;
                c[row][i] = '*';
                if (row < h - 1 && (times == 0 || times == 2))
                {
                    row += 1;
                    
                }
                else
                {
                    row -= 1;
                    
                }
                
            }
            string []str = new string[h];
            for(int i = 0; i < h; i ++)
            {
                str[i] = c[i].ToString();
               
            }
            
            return str;
          
        }

if I compile it in a VS i get no errors.Here is an example of the result

This task is taken from the Codewars but if I try to test on Codewars with the code I described above, I get this error

Edited:I changed returning array with "h" length to empty array and had got this

1 Answers

i found the task solution by replacing charters that equal '\0' with character ' ' here is working code

public string[] GetW(int h)
        {
            if (h < 2) return new string[]{};
            int row = 0;
            int stars_number = h * 4 - 3;
            int times = 0;
            StringBuilder[] c = new StringBuilder[h];
            for(int a = 0; a < h; a++)
            {
                c[a] = new StringBuilder();
                c[a].Length = stars_number;
            }
            for (int i = 0; i < stars_number; i++)
            {
                if (i == h - 1) times = 1;
                if (i == stars_number-2 * h + 1) times = 2;
                if (i == stars_number - h) times = 3;
                c[row][i] = '*';
                if (row < h - 1 && (times == 0 || times == 2))
                {
                    row += 1;
                    
                }
                else
                {
                    row -= 1;
                    
                }
                
            }
            string []str = new string[h];
            for(int i = 0; i < h; i ++)
            {
                c[i].Replace('\0', ' ');
                str[i] = c[i].ToString();
               
            }
            
            return str;
          
        }

And here is result of test enter image description here

Related