473,382 Members | 1,441 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Checking that a string contain only numeric characters

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.

Dec 4 '06 #1
7 15267
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.

Dec 4 '06 #2
bcpkh wrote:
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()

--
Peter

Dec 4 '06 #3
bcpkh wrote:
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
Dec 4 '06 #4
bcpkh wrote:
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.

Dec 5 '06 #5
bcpkh wrote:
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'.

--
Eric Sosman
esosman
Dec 5 '06 #6
bcpkh wrote:
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));
}

--
Simon.
Dec 5 '06 #7
Hello All

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

Regards,

B

Dec 5 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: John V | last post by:
What kind of regular expression pattern is needed to check if URL is valid? It's enought if most of cases are covered. I have PHP 4.x. Br
1
by: JuniorLinn | last post by:
Hi there - I would like to share this strip of code with our SQL 2000 DBA community. The code below strips all non-numeric characters from a given string field and rebuilds the string. Very...
6
by: Peter Afonin | last post by:
Hello, Should be a pretty simple question: I need to validate a string. It must be numeric and contain exactly 12 characters. I know how to do it in code, but I'd like to use a validation...
13
by: Paraic Gallagher | last post by:
Hi, This is my first post to the list, I hope somebody can help me with this problem. Apologies if it has been posted before but I have been internet searching to no avail. What I am trying...
12
by: Ima Loozer | last post by:
OK folks here is what I need help with. Lets assume I have a text field that will contain AlphaNumeric data. There is no set pattern to the field such that any given character can be either alpha...
7
by: Jay | last post by:
I want to check if a character is a decimal digit (0-9). I've found Char.IsNumber() and Char.IsDigit() but couldn't work out from Help how they differ. Also, do I need to worry about how digits...
42
by: =?Utf-8?B?UGxheWE=?= | last post by:
I have an if statement that isn't working correctly and I was wondering how I check for a blank string. My Code Example if me.fieldname(arrayIndex) = "" then ----- end if When I do this and...
14
by: nishit.gupta | last post by:
Is their any single fuction available in C++ that can determine that a string contains a numeric value. The value cabn be in hex, int, float. i.e. "1256" , "123.566" , "0xffff" , It can also...
3
by: amija0311 | last post by:
Hi, I am new using DB2 9.1 database by windows base. I want to query the data that contain string then translate the string into integer using DB2. The problems is If the data is null, i got the...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.