By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,670 Members | 1,462 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,670 IT Pros & Developers. It's quick & easy.

How to find string contains a numeric value

P: n/a
Is their any 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"
Thnx

Apr 4 '07 #1
Share this Question
Share on Google+
13 Replies


P: n/a
On Apr 4, 4:01 pm, nishit.gu...@st.com wrote:
Is their any 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"
Try strtol(). If that gives an error then try strtod(). If that also
gives an error, then the string does not contain a numeric value.


Apr 4 '07 #2

P: n/a
On Apr 3, 9:01 pm, nishit.gu...@st.com wrote:
Is their any 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"
If you want a C++ solution, then post in news:comp.lang.c++

One answer to your question is to write a formal grammar for numbers
and then use the grammar to write a parser.

You could do it by hand in C by using fuctions:
isdigit()
isxdigit()
ispunct()
isalpha()
and a bit of logic.

P.S.
It's harder than it looks to get it right.

Just because something is a number doesn't mean you can read it in
either.

For example:
1.0e99999

Apr 4 '07 #3

P: n/a
Thanx fr reply but what does an error means......
strtol & strtod returns 0 on failure and my string can contains zero
too i.e. "0"
So how will i differentiate???
and in strtol onre needs to give the base value, which i am not sure
it can be hex(16), decimal(10)???
Old Wolf wrote:
On Apr 4, 4:01 pm, nishit.gu...@st.com wrote:
Is their any 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"

Try strtol(). If that gives an error then try strtod(). If that also
gives an error, then the string does not contain a numeric value.
Apr 4 '07 #4

P: n/a
ni**********@st.com wrote:
Is their any 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"
If you tru;y want a C++ answer, you should post to a C++ newsgroup. We
don't do Fortran, COBOL, or Ada here, either.

If a C answer will suffice, have a look at the strto* family of
functions: strtod, strtof, strtold for floating point numbers;
strtoimax, strtol, strtoll, strtoul, strtoull, strtoumax for integral
numbers. In C these are prototyped in <stdlib.c>; for C++ the header
should be <cstdlib>, but check your C++ text to make sure. If you have
questions after checking your textbook (or implementation documentation)
ask in a C++ newsgroup, not here.
Apr 4 '07 #5

P: n/a
On Apr 4, 4:10 pm, nishit.gu...@st.com wrote:
Thanx fr reply but what does an error means......
strtol & strtod returns 0 on failure and my string can contains zero
too i.e. "0"
So how will i differentiate???
and in strtol onre needs to give the base value, which i am not sure
it can be hex(16), decimal(10)???
All your questions are answered by the documentation of strtol
and strtod. I tried a Google search and it found sufficient
documentation as the first hit for 'strtod'. You could also
look in the C Standard.

Apr 4 '07 #6

P: n/a
ni**********@st.com wrote:
Thanx fr reply but what does an error means......
Please don't top-post. Your replies belong following or interspersed
with properly trimmed quotes. See the majority of other posts in the
newsgroup, or:
<http://www.caliburn.nl/topposting.html>
Apr 4 '07 #7

P: n/a
On Apr 3, 10:43 pm, "Old Wolf" <oldw...@inspire.net.nzwrote:
On Apr 4, 4:10 pm, nishit.gu...@st.com wrote:
Thanx fr reply but what does an error means......
strtol & strtod returns 0 on failure and my string can contains zero
too i.e. "0"
So how will i differentiate???
and in strtol onre needs to give the base value, which i am not sure
it can be hex(16), decimal(10)???

All your questions are answered by the documentation of strtol
and strtod. I tried a Google search and it found sufficient
documentation as the first hit for 'strtod'. You could also
look in the C Standard.
Neither strtol() nor strtod() is capable of deciding whether a string
contains a numeric value.
They would be jim-dandy for turning a string into a numeric value,
once it has been determined that the string does contain one of the
proper format.
Apr 4 '07 #8

P: n/a
On Apr 3, 11:01 pm, nishit.gu...@st.com wrote:
Is their any 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"
Thnx
AFAIK, there is no *single* function that can recognize a numeric
string in any format. You can go one of two ways here: you can use a
combination of standard library functions (strtol(), strtod(),
strchr(), etc.) to attempt to convert the string to a numeric value,
and then check for any unconverted characters. The general algorithm
would be:

Strip leading whitespace
If the first character is '0' then
If the second character is 'x' or 'X' then
Convert the string using strtol(string, &check, 16)
where check is a pointer to a character
If *check is 0 or whitespace then
the string is an integer in hex format
Else
the string is not numeric
Else
Convert the string using strtol(string, &check, 8)
If *check is 0 or whitespace
the string is an integer in octal format
Else
the string is not numeric
End If
Else
If strpbrk(string, ".eE") is not null then
Convert the string using strtod(string, &check)
If *check is whitespace or 0 then
the string is a float in decimal
Else
the string is not numeric
End If
Else
Convert the string using strtol(string, &check, 10)
If *check is whitespace or 0 then
the string is an integer in decimal
Else
the string is not numeric
End If
End If
End If

Alternately, you can build a finite state machine that will scan the
string character by character; if the machine is in an accepting state
after scanning the whole string, then the string is numeric. This
isn't as straightforward a method as the above, and it can involve
quite a bit more work. The tradeoff is that you can accept strings in
any format you like, such as "1,234,567", where the method above
won't.

FSMs can get *very* tedious to write if you have a lot of states.
There are tools like lex or flex that will generate FSMs based on
rules you specify, but they take a little time to learn, and they're
not universally available. However, if you decide to expand your
rules for a valid numeric string, it's a helluva lot easier to edit
the specification file for lex than to try to edit the FSM code
itself.

Apr 4 '07 #9

P: n/a
On Apr 5, 9:01 am, "user923005" <dcor...@connx.comwrote:
Neither strtol() nor strtod() is capable of deciding whether a string
contains a numeric value.
C & V ?

Apr 4 '07 #10

P: n/a
On Apr 4, 4:20 pm, "Old Wolf" <oldw...@inspire.net.nzwrote:
On Apr 5, 9:01 am, "user923005" <dcor...@connx.comwrote:
Neither strtol() nor strtod() is capable of deciding whether a string
contains a numeric value.

C & V ?
You have to tell it where to start looking, and you have to tell it
what to look for.
I should think it rather obvious.

They aren't parsers. These functions convert what is supposed to be
'data in a specified format' at a 'specified location.'

Here is the original post:
"Is their any 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"
Thnx"

If we know where it is, and if we know what it is, then those
functions will correctly convert the value.
We might try a loop that steps along, one character at a time...
(we'll use strtod() since it will convert both integers and floating
point into a numeric value):

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

char string[65536];

double parse_number(char *s, int *err)
{
int value = 0;
char *endptr = s;
*err = 1;
if (s) {
char *t = endptr;
while (t) {
double d = strtod(t, &endptr);
if (endptr != t) {
*err = 0;
return d;
} else
t++;
}
}
*err = 1;
return DBL_MAX / DBL_MIN;
}
int main(void)
{
int err;
while (fgets(string, sizeof string, stdin)) {
double d = parse_number(string, &err);
if (err == 0) {
puts("String contains a number");

} else
puts("String does not contain a number.");
}
return 0;
}

C:\tmp>pars < t.txt
String contains a number

C:\tmp>type t.txt
hey d00dz! r u c00l like mee!

C:\tmp>

You tell me -- does the above string contain a number?

Apr 5 '07 #11

P: n/a
user923005 wrote:
"Old Wolf" <oldw...@inspire.net.nzwrote:
>On Apr 4, 4:10 pm, nishit.gu...@st.com wrote:
>>Thanx fr reply but what does an error means......
strtol & strtod returns 0 on failure and my string can contains
zero too i.e. "0"
So how will i differentiate???
and in strtol onre needs to give the base value, which i am not
sure it can be hex(16), decimal(10)???

All your questions are answered by the documentation of strtol
and strtod. I tried a Google search and it found sufficient
documentation as the first hit for 'strtod'. You could also
look in the C Standard.

Neither strtol() nor strtod() is capable of deciding whether a
string contains a numeric value. They would be jim-dandy for
turning a string into a numeric value, once it has been
determined that the string does contain one of the proper format.
To make squirrel pie, first catch your squirrel. To decide if
input is numeric, first define what is a numeric representation.
Base, digits, etc. Possibly signs, commas, blanks, tabs, line
endings, etc.

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>
<http://www.aaxnet.com/editor/edit043.html>

"A man who is right every time is not likely to do very much."
-- Francis Crick, co-discover of DNA
"There is nothing more amazing than stupidity in action."
-- Thomas Matthews

--
Posted via a free Usenet account from http://www.teranews.com

Apr 5 '07 #12

P: n/a
On Apr 5, 11:57 am, "user923005" <dcor...@connx.comwrote:
On Apr 4, 4:20 pm, "Old Wolf" <oldw...@inspire.net.nzwrote:
On Apr 5, 9:01 am, "user923005" <dcor...@connx.comwrote:
Neither strtol() nor strtod() is capable of deciding whether a string
contains a numeric value.
C & V ?

You have to tell it where to start looking, and you have to tell it
what to look for. I should think it rather obvious.

Here is the original post:
"Is their any fuction available in C++ that can determine that a
string contains a numeric value.
I read this to mean that the numeric value starts at the start
of the string; you appear to be reading it as if the OP wants
to extract 1234 from "abc1234def".

Apr 5 '07 #13

P: n/a
On Apr 4, 9:59 pm, "Old Wolf" <oldw...@inspire.net.nzwrote:
On Apr 5, 11:57 am, "user923005" <dcor...@connx.comwrote:
On Apr 4, 4:20 pm, "Old Wolf" <oldw...@inspire.net.nzwrote:
On Apr 5, 9:01 am, "user923005" <dcor...@connx.comwrote:
Neither strtol() nor strtod() is capable of deciding whether a string
contains a numeric value.
C & V ?
You have to tell it where to start looking, and you have to tell it
what to look for. I should think it rather obvious.
Here is the original post:
"Is their any fuction available in C++ that can determine that a
string contains a numeric value.

I read this to mean that the numeric value starts at the start
of the string; you appear to be reading it as if the OP wants
to extract 1234 from "abc1234def".
If the number definitely starts the string, then the problem is
clearly simpler (though the subject line clearly says otherwise). Of
course the original post was a bit incoherent so it is hard to know
what was really wanted. But even in the simple case, consider:

11111111111111111111111111111111111111111111111111 11111111111111
0xffffffffffffffffff
1e400
01777777777777777777777
1.2345678901234567890D037

Even the simple case isn't simple.

Apr 5 '07 #14

This discussion thread is closed

Replies have been disabled for this discussion.