473,725 Members | 2,251 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 14980
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...@inspir e.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

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

Similar topics

5
13857
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 check if the string contains more than 3 numeric characters because i must only process the ones with more than 3.
9
11232
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. I wanted to use a regular expression but I could not find an ‘AND’ operator within the regular expression – so I need something like match true if string contains ‘a’ and ‘b’ and ‘c’ and ‘d’ ..etc,etc. If anyone has any thoughts on how I can...
4
10816
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
2245
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 fields like CUST_ID, CASTE, AGE etc. 101 (12,89,76,34,66,90,36) 23 128 (27,45,71,211,43,107,67,451) 26 117 (11,23,32,265,142,88,67,98) 30...
14
2929
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 contain zero
6
2919
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 the wstring contains a numeric value. This is the actual conversion: double fValue = _wtof(strValue.c_str()); if strValue contains some characters (e.g.: 'aaa'), _wtof returns 0, therefore I do not really know that the input was initially...
2
2393
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 user if he types a 'numeric' value in place of a string and vice versa for the other cases. Give me a pointer as how to go about it? Thankz for your help in advance.
9
8244
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
9401
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9257
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9179
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8099
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6702
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6011
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4519
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4784
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2637
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.