0

Background:
Hi, I am currently learning C# and doing some practices at HackerRank. So I've come across a staircase problem which I should code to receive an integer input, then output the staircase 'diagram' using spaces and hashes.

Challenge faced:
The codes below give me a Runtime Error that says " System.ArgumentOutOfRangeException: Length cannot be less than zero. "

    // Complete the staircase function below.
    static void staircase(int n) {
        
        // Find number of spaces needed 
        string space = "";    
        for (int i = 1; i < n; i++) {
            space += " ";
        }

        string hash = "#";
        for (int j = 0; j < n; j++) {             
            space = space.Substring(0, space.Length - j);
            Console.WriteLine(space + hash);          
            hash += "#";      
        }

    }

However, when I change the code in the second for loop from

            space = space.Substring(0, space.Length - j);
            Console.WriteLine(space + hash);  

to Console.WriteLine(space.Substring(0, space.Length - j) + hash);

It then runs successfully, I see no difference and am confused why does it work?

sesame
  • 15
  • 6
  • 2
    `space = space.Substring(0, space.Length - j);` - With this code, you're constantly making `space` shorter, right? With just `Console.WriteLine(space.Substring(0, space.Length - j) + hash);` you're not changing `space`. – ProgrammingLlama Dec 23 '20 at 03:22
  • Yes, I wanted to make `space` shorter every loop. Thanks – sesame Dec 23 '20 at 06:33
  • If `space.Length` is `0` and `j` is (for example) `5`, what length of string do you suppose `space.Length - j` requests for this? `-5`, right? That's what I was pointing out. – ProgrammingLlama Dec 23 '20 at 06:37
  • Ah I see, that's my original confusion too! i tot after the first loop, (using `5` as input), `space.Length` would be equal to 5 ? (thks for helping me out) – sesame Dec 23 '20 at 06:48
  • You can see what happens [here](https://rextester.com/PMTB1227). You eventually end up with a `space.Length` value of `1` and a value `j = 3`, such that `space.Length - j = -2` – ProgrammingLlama Dec 23 '20 at 06:53
  • Appreciate it, sensei :) – sesame Dec 23 '20 at 07:16

1 Answers1

2

Those two are not the same.

This line:

Console.WriteLine(space.Substring(0, space.Length - j) + hash);

is similar to this:

string temporarySpace = space.Substring(0, space.Length - j);
Console.WriteLine(temporarySpace + hash);

In there, you are not modifying the variable space, instead, you are creating a temporary one

I changed your code so that you can see this behaviour: https://dotnetfiddle.net/ZhXIWa

for (int j = 0; j < n; j++) {             
    var tempSpace = space.Substring(0, space.Length - j);
        
    Console.Write($"space: {space.Length} | ");
    Console.Write($"tempSpace: {tempSpace.Length} | ");
    Console.Write(space + hash);
    Console.Write(Environment.NewLine);

    hash += "#";      
}

// Output

space: 9 | tempSpace: 9 |          #
space: 9 | tempSpace: 8 |          ##
space: 9 | tempSpace: 7 |          ###
space: 9 | tempSpace: 6 |          ####
space: 9 | tempSpace: 5 |          #####
space: 9 | tempSpace: 4 |          ######
space: 9 | tempSpace: 3 |          #######
space: 9 | tempSpace: 2 |          ########
space: 9 | tempSpace: 1 |          #########
space: 9 | tempSpace: 0 |          ##########

In there you will see how many characters both variables (space and temporarySpace) have. You can modify the code to the one that is not working and you will see the difference :)

Carlos Garcia
  • 2,771
  • 1
  • 17
  • 32