# Getting Integer from String?

Discussion in 'C Programming' started by jobo, Nov 7, 2006.

1. ### joboGuest

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.

jobo, Nov 7, 2006

2. ### Chris McDonaldGuest

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

Chris McDonald, Nov 7, 2006

3. ### Guest

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, 2006
4. ### Andrew PoelstraGuest

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.

Andrew Poelstra, Nov 7, 2006
5. ### Guest

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, 2006
6. ### Andrew PoelstraGuest

On Tue, 2006-11-07 at 07:29 -0800, 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.

Andrew Poelstra, Nov 7, 2006
7. ### Peter KarlssonGuest

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

strtol?

//Peter

Peter Karlsson, Jan 15, 2007
8. ### CBFalconerGuest

Peter Karlsson wrote:
> On 2006-11-07, jobo <> 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

CBFalconer, Jan 15, 2007
9. ### Xavier SerrandGuest

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

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

>
> strtol?
>
> //Peter

Xavier Serrand, Jan 15, 2007