String / Char Arrays are zero-based.
HelloWorld is 10 characters, but as an array we see it as:
H,e,l,l,o,W,o,r,l,d
0,1,2,3,4,5,6,7,8,9
Each character becomes an iteration of the loop.
When you use i = 0; i <= word.length
it is the same as i <= 10
.
Since the array is zero-based, the "less than or equal to" actually creates 11 iterations of the loop when we only have 10. We only have 10 characters or iterations, we cannot have 11. The first iteration value is "H" at array position 0 str[0]
hence declaring i = 0;
The last value is "d" at array position 9 str[9]
at iteration 10. Position 10, on iteration 11, str[10]
does not exist. It is out of bounds of the array.
You actually want i < 10
which would be i < word.length
. Again, we want to stop at 1 short of 10 because the array is Zero-based.