Check if a spelled number is in a range in C++

I believe it’s true that the input is acceptable if and only if either:

  • It is a prefix substring of the lower bound converted to string

or

  • The input followed by any number of additional zeros (possibly none) falls into the range

The first rule is required by e.g. 13 is in range (135, 140). The second rule is required by e.g. 2 is in range (1000, 3000).

The second rule can be efficiently tested by a series of multiplies by 10, until the scaled input exceeds the upper bound.

Leave a Comment