Velocity Reviews > Getting Integer from String?

# Getting Integer from String?

jobo
Guest
Posts: n/a

 11-07-2006
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.

Chris McDonald
Guest
Posts: n/a

 11-07-2006
"jobo" <(E-Mail Removed)> writes:

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

mark_bluemel@pobox.com
Guest
Posts: n/a

 11-07-2006

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.

Andrew Poelstra
Guest
Posts: n/a

 11-07-2006
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.

mark_bluemel@pobox.com
Guest
Posts: n/a

 11-07-2006

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

Andrew Poelstra
Guest
Posts: n/a

 11-07-2006
On Tue, 2006-11-07 at 07:29 -0800, http://www.velocityreviews.com/forums/(E-Mail Removed) 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.

Peter Karlsson
Guest
Posts: n/a

 01-15-2007
On 2006-11-07, jobo <(E-Mail Removed)> 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.

strtol?

//Peter

CBFalconer
Guest
Posts: n/a

 01-15-2007
Peter Karlsson wrote:
> On 2006-11-07, jobo <(E-Mail Removed)> wrote:
>>
>> 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

Xavier Serrand
Guest
Posts: n/a

 01-15-2007
long int convert (char * s, char *endptr, int base)
{
return strtol (s+3, &endptr, base);
}

"Peter Karlsson" <(E-Mail Removed)> a écrit dans le message de news:
eogc70\$d39\$(E-Mail Removed)...
> On 2006-11-07, jobo <(E-Mail Removed)> 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.

>
> strtol?
>
> //Peter