How to find string contains a numeric value

N

nishit.gupta

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
 
O

Old Wolf

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.
 
U

user923005

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
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
 
N

nishit.gupta

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)???
 
M

Martin Ambuhl

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.
 
O

Old Wolf

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.
 
U

user923005

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.
 
J

John Bode

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.
 
U

user923005


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?
 
C

CBFalconer

user923005 said:
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
 
O

Old Wolf

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".
 
U

user923005

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:

1111111111111111111111111111111111111111111111111111111111111111
0xffffffffffffffffff
1e400
01777777777777777777777
1.2345678901234567890D037

Even the simple case isn't simple.
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top