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

How to find string contains a numeric value

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
13 14929
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
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
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
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
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
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: ief | last post by:
hi all, i'm trying to check the length of a numeric value in a string. this is what i need to do: I have a string "Mystring (253)" and a string "SecondString (31548745754)" Now i have to...
9
by: booksnore | last post by:
I am writing some code to search for strings that contain every letter of the alphabet. At the moment I am using the method below to check to see if a string contains every letter of the alphabet....
4
by: Surek | last post by:
I need to get the numeric value alone in a string which includes special characters and alphabets. eg string: vism/t1-1/1 Should retrieve the first numeric value from the given string.
1
by: pchadha20 | last post by:
How to single numeric values from a field in a table which has multiple numeric value in a field of a table.And that table contains thousands of records. Suppose we have a table customer having...
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...
6
by: frohlinger | last post by:
Hi, I need to perform some numeric calculations on a numeric float value, that is received as wstring. I would like to perform a check before converting the wstring to float, checking that indeed...
2
by: Aaryan123 | last post by:
Hi... Am wondering as how I can fix this issue: I need to restirct the user, if he enter a 'string' in place of digit/numeric. Similary for the other cases, wherein which I need to restrict the...
9
by: engteng | last post by:
How do I convert string to numeric in VB.NET 2003 ? Example convert P50001 to 50001 or 50001P to 50001 but if P is in middle then not convert. Regards, Tee
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.