简体   繁体   中英

Check if the first element of an array is a digit in C

I was making a program to take a list of data and separate it by the "," in the file. However, some items had multiple commas.

Is there any efficient way of checking the first character of an array? For example:

char *array = {'1','A','C','D','5'};

Now, I want to only do somefunction(array) if array starts with a digit, even if it is in char format. So in this example, let somemethod be the way of determining if the first element is an integer:

char *array = {'1','A','C','D','5'};
if( somemethod ) someaction(array); 

How could I do this efficiently?

The first element of an array is array[0] so

if (isdigit(array[0])) someaction(array);

is what you're looking for.

The standard function isdigit from <ctype.h> could be also efficient:

#include <ctype.h>

if (isdigit((unsigned char)array[0])) someaction(array);

You can also define your own, with just two comparisons:

#if (!defined __STDC_VERSION__) || (__STDC_VERSION__ < 199901L)
# undef inline
# undef bool
# define inline 
# define bool int
#endif

inline bool somemethod(const unsigned char c) { return c >= '0' && c <= '9'; }

However, thinking about performances here looks like premature optimization .

Not sure what you're getting at with the efficiency drive, but there's a really easy way to do this:

_Bool doesBeginWithNumber( char *input ) {
    return input[0] >= '0' && input[0] <= '9';
}

It checks the ASCII value of the first character. If it is between 0 and 9, it is a number.

Another way is to use a lookup table, which can be more efficient if you have a more complex check (but for numbers it is probably slower — benchmark to find out!):

_Bool doesBeginWithNumber( char *input ) {
    const static _Bool cases[256] = {
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
    }
    return cases[(unsigned char) input];
}

And another option, as pointed out by others, is good old standardised isdigit :

_Bool doesBeginWithNumber( char *input ) {
    return isdigit( input[0] );
}

isdigit参考页面已经包含一个工作示例,基本上isdigit您的需求。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM