Commit f55e16cf authored by Archit Tamarapu's avatar Archit Tamarapu
Browse files

update is_number() function

parent cc6048c3
Loading
Loading
Loading
Loading
+43 −4
Original line number Diff line number Diff line
@@ -62,7 +62,7 @@ char *to_upper( char *str )
 * Check if a string contains only digits.
 *---------------------------------------------------------------------*/

bool is_digits_only( char *str )
bool is_digits_only( const char *str )
{
    int16_t i;

@@ -86,19 +86,58 @@ bool is_digits_only( char *str )
 * Check if a string is a number.
 *---------------------------------------------------------------------*/

bool is_number( char *str )
bool is_number( const char *str )
{
    int16_t i;
    int16_t decimal_separator_count;
    int16_t numeric_len;

    i = 0;
    decimal_separator_count = 0;
    numeric_len = 0;

    /* Check for null string or sign character */
    if ( str[i] == '\0' )
    {
        return false;
    }
    else if ( str[i] == '+' || str[i] == '-' )
    {
        i++;
    }

    /* Ensure rest of string is numeric and only one decimal separator is present */
    while ( str[i] != 0 )
    {
        if ( ( str[i] < '0' || str[i] > '9' ) && str[i] != '.' && str[i] != '-' && str[i] != '\n' && str[i] != '\r' )
        if ( str[i] < '0' || str[i] > '9' )
        {
            if ( str[i] == '.' )
            {
                if ( decimal_separator_count > 1 )
                {
                    return false;
                }
                else
                {
                    decimal_separator_count++;
                }
            }
            else if ( str[i] != '\r' && str[i] != '\n' )
            {
                return false;
            }
        }
        else
        {
            numeric_len++;
        }
        i++;
    }

    if ( numeric_len == 0 )
    {
        return false;
    }

    return true;
}
+2 −2
Original line number Diff line number Diff line
@@ -36,9 +36,9 @@
#include <stdbool.h>
#include <stdint.h>

bool is_digits_only( char *str );
bool is_digits_only( const char *str );

bool is_number( char *str );
bool is_number( const char *str );

char *to_upper( char *str );