You can test for the "length" of input. i.e. if input is 5 then loop only 5 times. If 4 then loop only 4. Assign the length a variable and test against that variable.