Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Getting Integer from String?

Reply
Thread Tools

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.

 
Reply With Quote
 
 
 
 
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.
 
Reply With Quote
 
 
 
 
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.

 
Reply With Quote
 
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.

 
Reply With Quote
 
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...

 
Reply With Quote
 
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.

 
Reply With Quote
 
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
 
Reply With Quote
 
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


 
Reply With Quote
 
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



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How do I add an Integer to another Integer? Sebastian Stelzer Java 6 04-07-2010 07:03 PM
CType(x,Integer) vs. Integer.Parse(x) =?Utf-8?B?Sm9l?= ASP .Net 7 02-07-2006 02:30 AM
how do I make Class.forName("Integer") returning java.lang.Integer? Johannes Zellner Java 22 12-19-2005 11:22 AM
How do I add an Integer to another Integer? Sebastian Stelzer Java 2 10-15-2004 01:17 PM
No Math.min(Integer, Integer)? =?ISO-8859-1?Q?Thomas_Gagn=E9?= Java 0 07-29-2003 07:46 PM



Advertisments