473,399 Members | 3,106 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,399 software developers and data experts.

Getting Integer from String?

Hello,

If I'm given a string that always starts with the word "free" followed
by an integer ("free xx"). How do I parse through the characters and
get to the integer value? Thanks.

Nov 7 '06 #1
8 6847
"jobo" <jo*****@gmail.comwrites:
>If I'm given a string that always starts with the word "free" followed
by an integer ("free xx"). How do I parse through the characters and
get to the integer value? Thanks.
Sounds like homework, but
If char *str = "free 123";
then the address of the '1' will be str+5, or &str[5] .

--
Chris.
Nov 7 '06 #2

jobo wrote:
Hello,

If I'm given a string that always starts with the word "free" followed
by an integer ("free xx"). How do I parse through the characters and
get to the integer value? Thanks.
Is there always a space between "free" and the integer?

Do you know for certain that there will be no characters after the
integer?

If so, then if the string is provided as a char * called (e.g.) "str"
then the integer (as a string) will be at str+5 and you could use one
of the standard functions (from the manual) to convert it to an integer
or long datatype.

There are other approaches, for example there is a function (also in
the manual) for scanning strings and extracting data from them. Or you
could write a routine that searches through the string till it find the
first digit, and then continues, multiplying a running total by 10 (I
presume the integer is in decimal representation) and adding the value
of the next digit, till you run out of digits.

Nov 7 '06 #3
On Mon, 2006-11-06 at 22:08 -0800, jobo wrote:
Hello,

If I'm given a string that always starts with the word "free" followed
by an integer ("free xx"). How do I parse through the characters and
get to the integer value? Thanks.
scanf("free %d", &n);

Any good C textbook should tell you that.

--
Andrew Poelstra <http://www.wpsoftware.net>
For email, use 'apoelstra' at the above site.
"You're only smart on the outside." -anon.

Nov 7 '06 #4

Andrew Poelstra wrote:
On Mon, 2006-11-06 at 22:08 -0800, jobo wrote:
Hello,

If I'm given a string that always starts with the word "free" followed
by an integer ("free xx"). How do I parse through the characters and
get to the integer value? Thanks.

scanf("free %d", &n);
Actually for a string, he needs sscanf() surely...

Nov 7 '06 #5
On Tue, 2006-11-07 at 07:29 -0800, ma**********@pobox.com wrote:
Andrew Poelstra wrote:
On Mon, 2006-11-06 at 22:08 -0800, jobo wrote:
Hello,
>
If I'm given a string that always starts with the word "free" followed
by an integer ("free xx"). How do I parse through the characters and
get to the integer value? Thanks.
>
scanf("free %d", &n);

Actually for a string, he needs sscanf() surely...
I misread being "given" a string as input from stdin. My bad.

--
Andrew Poelstra <http://www.wpsoftware.net>
For email, use 'apoelstra' at the above site.
"You're only smart on the outside." -anon.

Nov 7 '06 #6
On 2006-11-07, jobo <jo*****@gmail.comwrote:
Hello,

If I'm given a string that always starts with the word "free" followed
by an integer ("free xx"). How do I parse through the characters and
get to the integer value? Thanks.
strtol?

//Peter
Jan 15 '07 #7
long int convert (char * s, char *endptr, int base)
{
return strtol (s+3, &endptr, base);
}

"Peter Karlsson" <me@privacy.neta écrit dans le message de news:
eo**********@news.al.sw.ericsson.se...
On 2006-11-07, jobo <jo*****@gmail.comwrote:
Hello,

If I'm given a string that always starts with the word "free" followed
by an integer ("free xx"). How do I parse through the characters and
get to the integer value? Thanks.

strtol?

//Peter

Jan 15 '07 #8
Peter Karlsson wrote:
On 2006-11-07, jobo <jo*****@gmail.comwrote:
>>
If I'm given a string that always starts with the word "free"
followed by an integer ("free xx"). How do I parse through the
characters and get to the integer value? Thanks.

strtol?
There are few things stupider than an erroneous response to a two
months old query.

--
"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
Jan 15 '07 #9

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

Similar topics

0
by: lglmi | last post by:
'********************START OF BAS MODULE******************** Option Explicit Private Type OSVERSIONINFO dwOSVersionInfoSize As Long dwMajorVersion As Long dwMinorVersion As Long...
7
by: Sashi | last post by:
Two questions: (1) I can pull the text of an XML element as a string just fine using code as such: strSomeString = myXmlDoc.SelectSingleNode("/Element1/Element2/Element3",...
15
by: sara | last post by:
Hi I'm pretty new to Access here (using Access 2000), and appreciate the help and instruction. I gave myself 2.5 hours to research online and help and try to get this one, and I am not getting...
21
by: Gerry Abbott | last post by:
Hi All, If completed a script which parses a string into fragments (fields), then assigns these substrings into an array. I wish to turn this into a function to which i can pass the string....
5
by: Nathan Sokalski | last post by:
I have a user control that contains three variables which are accessed through public properties. They are declared immediately below the "Web Form Designer Generated Code" section. Every time an...
4
by: ItsMe | last post by:
Hi Guyz, I'm unable to understand this (AddressOf) error??? In VB6 I have two functions: ---------------------------- Public Function ImageFirstImageCallback(ByVal hWnd As Integer, ByVal...
2
by: pangel83 | last post by:
I've been trying for days to write a piece of VB.NET code that will read from winamp's memory space the paths of the files from the current winamp playlist. The GETPLAYLISTFILE command of the...
0
by: TG | last post by:
Hi! Once again I have hit a brick wall here. I have a combobox in which the user types the server name and then clicks on button 'CONNECT' to populate the next combobox which contains all the...
1
by: raghuvendra | last post by:
Hi I have a jsp page with 4 columns: namely Category name , Category order, Input field and a submit button. All these are aligned in a row. And Each Category Name has its corresponding Category...
5
by: tshad | last post by:
I have the following class in my VS 2008 project that has a namespace of MyFunctions. ********************************* Imports System Imports System.Text.RegularExpressions Namespace...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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,...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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...

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.