I would recommend testing your code with the following input string: "My hand hurts".
The code you presented won't work properly for this - I'll leave it to you to find out why :)