I suggest you take a trivial length password, e.g. 3 characters, and step through your code by hand with pencil and paper, keeping track of the variables. It shouldn't take you more than a few...