0
String input = stack.pop();

if ((index = input.indexOf('*')) != -1) {

    for (char c = '0'; c <= '1'; c++) {
        input = input.substring(0, index) + c +
            input.substring(index + 1);
        stack.push(input);
    }
}

I'm struggling to understand what's happening in the for loop. Can someone clarify how it iterates? I'm not used to having something other than the length of the array as the second parameter.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
  • Yes, it reads a string from the stack and if this string contains `*`, then two strings are pushed back to the stack - one with `0` in place of `*`, the other with `1`. – Nowhere Man Sep 26 '20 at 19:58
  • You can read what a for loop means and rewrite it into a while loop to understand it better: https://en.cppreference.com/w/cpp/language/for https://stackoverflow.com/a/26034436/ – user202729 Sep 27 '20 at 12:14

1 Answers1

0

When you write for loop like this c have value = 48 accord to ascii table when you increment c it go up to 49 which is equal to 1 also from ascii. So now if you know that you can change your code to this

for (int c = 48; c <= 49; c++)
{
    //your code
}

And it is the same. But if you concatenate string like you did value 48 is convert to 0,so * is gonna be replace with 0, in next iteration will be replace with 1.

Boorsuk
  • 291
  • 1
  • 10