I have a pointer to a char array. I want to increase it until it will not point at a digit / number (represented as a char in the char array).
For example, if pointer points to '2' in this char array:
['1'][' ']['2']['3']['4'][' '][' '][' ']['5']['6']['7']
^
*pointer
I want to increase it until it will point at the first non-digit character – ' ':
['1'][' ']['2']['3']['4'][' '][' '][' ']['5']['6']['7']
^
*pointer
I know I can do something like this:
while (*pointer == '0' || *pointer == '1' || *pointer == '2' || ...)
pointer++;
return pointer;
but it’s pretty long and not elegant at all.
I thought I could use atoi(), which returns 0 when the pointer doesn’t point at a number:
while (atoi(pointer) != 0 || *pointer == '0') //while it still points at a number
pointer++; //increase the pointer until it will not point at a number
return pointer;
But it doesn’t seem to work. Maybe it’s okay and I have a mistake somewhere else, but anyway I wanted to know:
Are there are any other (better) ways to check whether a pointer to a char array points at a digit / number and increase it until it will point at a non-digit character, in C?
You should probably use
isdigitfromctype.hinstead. Something like: