473,322 Members | 1,614 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,322 software developers and data experts.

scanning string for ![0..9]

Is there a better way to check if a char* contains
characters that are not decimal numbers?

TIA

// quickly scan str, return if something is outside [0..9]
count = 0;
while (str[count] != '\0') {
if ((str[count] < '0') || (str[count] > '9')) {
return INVALID_CHAR;
}
count++;
}
Nov 14 '05 #1
9 1909
aurgathor wrote:

Is there a better way to check if a char* contains
characters that are not decimal numbers?
There's another way.
I don't know if it's better.
// quickly scan str, return if something is outside [0..9]
count = 0;
while (str[count] != '\0') {
if ((str[count] < '0') || (str[count] > '9')) {
return INVALID_CHAR;
}
count++;
}


#include <ctype.h>

for (count = 0; str[count] != '\0'; ++count) {
if (!isdigit((unsigned char)str[count])) {
return INVALID_CHAR;
}
}

--
pete
Nov 14 '05 #2
"aurgathor" <sp********@you.com> wrote in message
news:1111230253.50c5ef0f6fe03ada1e7834f618195e3a@t eranews...
Is there a better way to check if a char* contains
characters that are not decimal numbers?

TIA

// quickly scan str, return if something is outside [0..9]
count = 0;
while (str[count] != '\0') {
if ((str[count] < '0') || (str[count] > '9')) {
return INVALID_CHAR;
}
count++;
}


Alternative, debatable if it's better:

#include <ctype.h>

char *p = str;
while (*p)
if (!isdigit((unsigned char)*p++)) return INVALID_CHAR;

Alex
Nov 14 '05 #3
aurgathor wrote:

Is there a better way to check if a char* contains
characters that are not decimal numbers?

// quickly scan str, return if something is outside [0..9]
count = 0;
while (str[count] != '\0') {
if ((str[count] < '0') || (str[count] > '9')) {
return INVALID_CHAR;
}
count++;
}


Don't use // comments in newsgroups. They don't survive line
wrapping very well, and are illegal in the most prevalent standard,
C90.

#include <ctype.h>
...
int digitsonly(char *str) {
while (*str)
if (!(isdigit(unsigned char)*str++)) return INVALID_CHAR;
return ALL_DIGS;
}

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson

Nov 14 '05 #4
"aurgathor" <sp********@you.com> wrote in message
Is there a better way to check if a char* contains
characters that are not decimal numbers?

strcspn(str, "0123456789")
--
Tor <torust AT online DOT no>

Nov 14 '05 #5
aurgathor wrote:
Is there a better way to check if a char* contains
characters that are not decimal numbers?

TIA

// quickly scan str, return if something is outside [0..9]
count = 0;
while (str[count] != '\0') {
if ((str[count] < '0') || (str[count] > '9')) {
return INVALID_CHAR;
}
count++;
}


It depends on what you mean by "better". Shorter? Faster?
int count = 0;
sscanf(str, "%*[0123456789]%n", &count);
if (str[count] != '\0)
return INVALID_CHAR;

--
Best regards,
Andrey Tarasevich

Nov 14 '05 #6
"aurgathor" <sp********@you.com> wrote in message
news:1111230253.50c5ef0f6fe03ada1e7834f618195e3a@t eranews...
Is there a better way to check if a char* contains
characters that are not decimal numbers?

TIA

// quickly scan str, return if something is outside [0..9]
count = 0;
while (str[count] != '\0') {
if ((str[count] < '0') || (str[count] > '9')) {
return INVALID_CHAR;
}
count++;
}


Take a look at isdigit(). Be sure to #include <ctype.h>.

S

--
Stephen Sprunk "Stupid people surround themselves with smart
CCIE #3723 people. Smart people surround themselves with
K5SSS smart people who disagree with them." --Aaron Sorkin

Nov 14 '05 #7
>"aurgathor" <sp********@you.com> wrote in message
Is there a better way to check if a char* contains
characters that are not decimal numbers?

In article <AQ******************@news2.e.nsc.no>
Tor Rustad <to****@online.no.spam> wrote:strcspn(str, "0123456789")


This will only count leading non-digits. For instance, the result
for "abc123def" will be 3, but for "123abcdef" it will be 0.

A somewhat twisted method is to use strtol or strtoul in combination
with strspn or strcspn to check for leading whitespace and/or sign:

int has_nondigit;
char *ep;

has_nondigit = strcspn(str, "0123456789") != 0 ||
(strtoul(str, &ep, 10), *ep != '\0');
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Nov 14 '05 #8
Chris Torek wrote:
"aurgathor" <sp********@you.com> wrote in message
Is there a better way to check if a char* contains
characters that are not decimal numbers?

In article <AQ******************@news2.e.nsc.no>
Tor Rustad <to****@online.no.spam> wrote:
strcspn(str, "0123456789")

This will only count leading non-digits. For instance, the result
for "abc123def" will be 3, but for "123abcdef" it will be 0.

A somewhat twisted method is to use strtol or strtoul in combination
with strspn or strcspn to check for leading whitespace and/or sign:

int has_nondigit;
char *ep;

has_nondigit = strcspn(str, "0123456789") != 0 ||
(strtoul(str, &ep, 10), *ep != '\0');


A possibly simpler approach that more closely matches
the problem statement might be

has_nondigit = str[ strspn(str, "0123456789") ] != '\0';

At about this point, though, I think I'd take a step
back and ask why the O.P. wants to determine whether a
string consists only of digits. If the intent is to convert
to a number, it's probably simpler to go ahead and convert
with strtol() or strtoul() and check that it succeeded and
converted the entire string; the conversion will detect and
report any non-digit it encounters.

Sometimes (not always, but sometimes) solving the stated
problem is less useful than coming up with a better statement.

--
Eric Sosman
es*****@acm-dot-org.invalid
Nov 14 '05 #9
"Chris Torek" <no****@torek.net> wrote in message
"aurgathor" <sp********@you.com> wrote in message
Is there a better way to check if a char* contains
characters that are not decimal numbers?


In article <AQ******************@news2.e.nsc.no>
Tor Rustad <to****@online.no.spam> wrote:
strcspn(str, "0123456789")


This will only count leading non-digits. For instance, the result
for "abc123def" will be 3, but for "123abcdef" it will be 0.


True, if the lenght of digits is wanted

strspn(str, "0123456789")

--
Tor <torust AT online DOT no>

Nov 14 '05 #10

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

Similar topics

21
by: CHANGE username to westes | last post by:
What are the most popular, and well supported, libraries of drivers for bar code scanners that include a Visual Basic and C/C++ API? My requirements are: - Must allow an application to be...
0
by: Ajay | last post by:
hi! I am developing an application for a pocket pc. i have a small gui which allows users to select files using a file dialog box. however the file, selected is represented as '/My...
0
by: Brent Burkart | last post by:
I am using a streamreader to read a log file into my application. Now I want to be able to scan for error messages, such as "failed", "error", "permission denied", so I can take action such as...
3
by: Brent Burkart | last post by:
I am using a streamreader to read a log file into my application. Now I want to be able to scan for error messages, such as "failed", "error", "permission denied", so I can take action such as...
2
by: excite | last post by:
I am trying to write a long string into a txt file, the string includes some backslash, looks like: 'd:\#########\#######\####### d:\######\####\####\### d:\###\###\' then I got a error saying...
2
by: jgbid | last post by:
Hi, I'm trying to build an IP Scanner inc c# for a specific port (80) and for specific IP Ranges. For example 24.36.148.1 to 24.36.148.255 My first step was to use TcpClient, but there are...
2
by: nmrpa91290 | last post by:
Hi, I am in the design phase of building a database that will track the productivity of my warehouse. I am thinking of using a barcode scanner to assist me with this. Employees will hopefully...
1
kirubagari
by: kirubagari | last post by:
For i = 49 To mfilesize Step 6 rich1.SelStart = Len(rich1.Text) rich1.SelText = "Before : " & HexByte2Char(arrByte(i)) & _ " " & HexByte2Char(arrByte(i + 1)) & " " _ &...
23
by: Rotsey | last post by:
Hi, I am writing an app that scans hard drives and logs info about every fine on the drive. The first iteration of my code used a class and a generic list to store the data and rhis took...
2
by: iheartvba | last post by:
Hi Guys, I have been using EzTwain Pro to scan documents into my access program. It allows me to specify the location I want the Doc to go to. It also allows me to set the name of the document...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
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...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
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...

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.