The reason it prints 12 is that there are empty strings in between every letter, and at both sides. Here is a diagram:
All empty strings!
^h^e^l^l^o^ ^w^o^r^l^d^
it looks weird, but every ^
is an empty sn empty string, and if you count them, there are 12.
The reason you are getting the error is that a string is just an array of characters, so it is zero-indexed, meaning that the first element is index 0, the second at index 1, and so on. Here is a diagram:
-------------------------------------
a | b | c | d | e | f | g | h | i | j
-------------------------------------
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9
-------------------------------------
As you can see, the tenth element (j
), is at index 9, so trying to get index 10 would result in an error.