Works for me. All I did was hardcode the string to run it online.

There are a number of style issues, like poor variable names and awkward program flow, but the function seems to be correct.