illuminati

1 min

Valid Number InterviewBit Solution

Problem: Valid Number

Problem Description:

Validate if a given string is numeric.

Examples:

  • "0" => true

  • " 0.1 " => true

  • "abc" => false

  • "1 a" => false

  • "2e10" => true

Return 0 / 1 ( 0 for false, 1 for true ) for this problem

Clarify the question using “See Expected Output”

  • Is 1u ( which may be a representation for unsigned integers valid?
     
    For this problem, no.

  • Is 0.1e10 valid?
     
    Yes

  • -01.1e-10?
     
    Yes

  • Hexadecimal numbers like 0xFF?
     
    Not for the purpose of this problem

  • 3. (. not followed by a digit)?
     
    No

  • Can exponent have decimal numbers? 3e0.1?
     
    Not for this problem.

  • Is 1f ( floating point number with f as prefix ) valid?
     
    Not for this problem.

  • How about 1000LL or 1000L ( C++ representation for long and long long numbers )?
     
    Not for this problem.

  • How about integers preceded by 00 or 0? like 008?
     
    Yes for this problem

Solution Approach

First, try to find if it has any character other than digits or not.

If it doesn't have any character other than a digit, you can return true.

If it has some characters in it, try to check whether it denotes a sign, exponent or decimal. If yes then you are good to go return true, otherwise false.

Solution:

In C++