The problem asks for determining if a given string is numeric. The challenge part of the problem is to consider each and every corner cases.
- If the string starts from number, it returns true;
- If the string starts from empty spaces, skip all the empty spaces;
- If the string starts from non-numbers, return false;
- If the string has white space in between, return false;
- If the first part of the string is numerical, return true.
Based on the analysis above, we can solve it the problem by the following steps:
- First check if the string is null or empty. Return false if the string is null or empty
- First check the leading white spaces, simply skip them. Be careful if the string contains only white spaces
- Then check the sign next, if it is "+" or '-', go to next;
- Check the dot character ' . '
- The next character must be a numerical number. If not, simply return false.
- Iterate until we reach the first non-numerical character in the string. Note that "1 " is true by "1 a" is false;