Checking that a string contain only numeric characters

B

bcpkh

Hello All

I need to check a string to make sure it does not contain any non
numeric characters, the problem that I face is that the string is
fairly long, 2784601121574585949, strtol etc. can't process this
because much bigger than long obviously.

Is there a way to check this string without resorting to scanning
through the entire string a character at a time?

Any suggestions would be much appreciated.
 
L

loic-dev

Hi,
I need to check a string to make sure it does not contain any non
numeric characters, the problem that I face is that the string is
fairly long, 2784601121574585949, strtol etc. can't process this
because much bigger than long obviously.

Is there a way to check this string without resorting to scanning
through the entire string a character at a time?

Any suggestions would be much appreciated.

/strtoll()/ perharps? I guess, independently of the solution chosen, at
the end of the day, you end-up in scanning through the entire string
character at a time (either a library's function does it for you, or
you do it your own).

Cheers,
Loic.
 
P

Peter Nilsson

bcpkh said:
Hello All

I need to check a string to make sure it does not contain any non
numeric characters, the problem that I face is that the string is
fairly long, 2784601121574585949, strtol etc. can't process this
because much bigger than long obviously.

Did you read the specs for strtol()? It certainly can process such
a string, even if the value would be outside the range of long.
[Hint: check the middle parameter.]
Is there a way to check this string without resorting to scanning
through the entire string a character at a time?

Any suggestions would be much appreciated.

Hint: strspn()
 
D

Default User

bcpkh said:
Hello All

I need to check a string to make sure it does not contain any non
numeric characters, the problem that I face is that the string is
fairly long, 2784601121574585949, strtol etc. can't process this
because much bigger than long obviously.

Is there a way to check this string without resorting to scanning
through the entire string a character at a time?

Any suggestions would be much appreciated.


What's wrong with scanning it? Almost any other method is going to use
some sort of scan under the hood anyway. If all you want to do is check
for a non-digit, just do it. That's the simplest and most clear method.

If for some reason that turns out to be some sort of bottleneck for the
program (unlikely) THEN look for some other method.





Brian
 
S

santosh

bcpkh said:
Hello All

I need to check a string to make sure it does not contain any non
numeric characters, the problem that I face is that the string is
fairly long, 2784601121574585949, strtol etc. can't process this
because much bigger than long obviously.

Is there a way to check this string without resorting to scanning
through the entire string a character at a time?

Any suggestions would be much appreciated.

Look, one way or the other, that's what happens behind the scenes. Just
use isdigit() or isxdigit() and worry about speed, only when it turns
out to be too slow.
 
E

Eric Sosman

bcpkh said:
Hello All

I need to check a string to make sure it does not contain any non
numeric characters, the problem that I face is that the string is
fairly long, 2784601121574585949, strtol etc. can't process this
because much bigger than long obviously.

Is there a way to check this string without resorting to scanning
through the entire string a character at a time?

No, because any character that isn't scanned might be
a '?' or a 'X' or a '\033'.

However, the fact that *someone* must scan every character
doesn't mean that *you* have to do the scanning!

if (strspn(string, "0123456789") == strlen(string))

/* Ugh; that's two scans. Instead, consider: */
if (string[strspn(string, "0123456789")] == '\0')

Note that the empty string "" contains no non-numeric
characters. If you wish to reject it, add a test for
strlen(string) != 0, or better, string[0] != '\0'.
 
S

Simon Biber

bcpkh said:
Hello All

I need to check a string to make sure it does not contain any non
numeric characters, the problem that I face is that the string is
fairly long, 2784601121574585949, strtol etc. can't process this
because much bigger than long obviously.

Is there a way to check this string without resorting to scanning
through the entire string a character at a time?

Any method you use will involve scanning through the string. That can be
done as part of the operation of a standard library function, or you can
do it manually with a loop construct. It probably won't make much
difference either way.
Any suggestions would be much appreciated.

This solution uses standard library functions to avoid explicitly coding
a loop. It is not the most efficient solution, since it does scan
through the string twice, once for strspn and once for strlen.

#include <string.h>
#include <stdio.h>

....

const char *str = "2784601121574585949";
const char *digits = "0123456789";

size_t digit_length = strspn(str, digits);
size_t str_length = strlen(str);

if(digit_length == str_length)
{
printf("only digits\n");
}
else
{
printf("non-digit character at %d\n", (int)digit_length);
}

Here's an alternative with a loop. It's more concise and elegant, and
only scans the string once. Notice the empty loop body. The entire
purpose of the loop is to advance the pointer p to a particular
location, the first non-digit location. If the string is all digits,
then that location will be the null character at the end of the string.
There's no need to explicitly test for the end of the string in the loop
condition, since isdigit(0) will return 0.

#include <ctype.h>
#include <stdio.h>

....

const char *str = "2784601121574585949";
const char *digits = "0123456789";

const char *p;
for(p = str; isdigit(*p); p++);

if(*p == '\0')
{
printf("only digits\n);
}
else
{
printf("non-digit character at %d\n", (int)(p - str));
}
 
B

bcpkh

Hello All

Thanks for all the great information - you guys really helped me.

Regards,

B
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top