Could you show us what the actual string is that it's supposedly wrong about? Checking what the value of text is at the time that loop starts should be trivial.
Just a blind guess, but it almost sounds like you might be getting confused by zero-indexing. Namely, if the string is 39 characters long then size() returns 39 and the valid indices are 0 through 38, not 1 through 39.