Getting Integer from String?

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

  1. jobo

    jobo Guest

    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
    #1
    1. Advertising

  2. "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
    #2
    1. Advertising

  3. jobo

    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
    #3
  4. 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
    #4
  5. jobo

    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
    #5
  6. 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
    #6
  7. 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
    #7
  8. jobo

    CBFalconer Guest

    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
    #8
  9. 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
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?Sm9l?=

    CType(x,Integer) vs. Integer.Parse(x)

    =?Utf-8?B?Sm9l?=, Feb 6, 2006, in forum: ASP .Net
    Replies:
    7
    Views:
    5,939
    =?Utf-8?B?RGF2aWQgQW50b24=?=
    Feb 7, 2006
  2. =?ISO-8859-1?Q?Thomas_Gagn=E9?=

    No Math.min(Integer, Integer)?

    =?ISO-8859-1?Q?Thomas_Gagn=E9?=, Jul 29, 2003, in forum: Java
    Replies:
    0
    Views:
    495
    =?ISO-8859-1?Q?Thomas_Gagn=E9?=
    Jul 29, 2003
  3. Sebastian Stelzer

    How do I add an Integer to another Integer?

    Sebastian Stelzer, Oct 14, 2004, in forum: Java
    Replies:
    2
    Views:
    481
    Yu SONG
    Oct 15, 2004
  4. Replies:
    2
    Views:
    282
  5. Randy Kramer
    Replies:
    12
    Views:
    360
    Robert Klemme
    Oct 25, 2007
Loading...

Share This Page