Check if a spelled number is in a range in C
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
If a number arrives as English words such as "forty two" or "one hundred five", you should solve the problem in two phases: parse the words into an integer, then compare that integer against the target range. The range check is easy. The real challenge is building a parser that accepts the specific vocabulary and grammar you care about.
Parse First, Compare Second
Trying to decide whether a spelled-out number is "between" two values without converting it first makes the code much harder than necessary. Once you convert the phrase into an integer, the range logic becomes ordinary integer comparison.
That means the implementation should look like this:
- normalize the input text,
- parse the number words,
- return an integer,
- check
min <= value <= max.
A Small English Number Parser
For common values, a direct word-to-number parser is enough. The example below supports basic units, tens, hundreds, and thousands.
This keeps parsing and range-checking responsibilities separate.
Normalize Input Before Parsing
Real input is often messier than simple examples. You may encounter:
- hyphenated words such as
"twenty-one", - uppercase input such as
"Ninety", - filler words such as
"and"in"one hundred and five".
Normalize before tokenizing.
If your domain allows "and", strip it during normalization or treat it as a token that does nothing.
Range Checking Is Trivial Once Parsed
After conversion, the range test is ordinary numeric logic.
That is why correctness depends mostly on the parser, not the comparison.
Define Your Supported Grammar Explicitly
Do not pretend the parser handles all English number phrases unless it truly does. Requirements can expand quickly:
- negative numbers,
- decimals,
- millions or billions,
- ordinals such as
"first"or"twentieth".
For production code, document the supported forms and reject the rest clearly. A strict parser is safer than a permissive one that guesses wrong.
Common Pitfalls
- Comparing word strings directly instead of converting them first.
- Ignoring normalization for hyphens and capitalization.
- Accepting unsupported tokens silently and producing wrong values.
- Forgetting that
"hundred"and"thousand"modify the running value rather than add like normal words. - Expanding input requirements without updating grammar rules deliberately.
Summary
- Parse the spelled-out number into an integer before doing any range logic.
- Normalize input so token matching stays simple and reliable.
- Keep the vocabulary and grammar explicit.
- Reject unsupported input clearly rather than guessing.
- Most of the problem is language parsing, not numeric comparison.

