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

Getting Integer from String?

P: n/a
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
Share this Question
Share on Google+
8 Replies


P: n/a
"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

P: n/a

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

P: n/a
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

P: n/a

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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.