Thread: grabbing an int from a string

  1. #1
    Registered User
    Join Date
    Apr 2007

    grabbing an int from a string

    now i have a problem

    if there is an equation like 12+13 in a string , how can i grab the int 12 and int 13 , each one alone

    not int 1 , int 2

    thanks in advance

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Rishon LeZion, Israel
    strtol for example
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    David J. Wheeler

  3. #3
    Lean Mean Coding Machine KONI's Avatar
    Join Date
    Mar 2007
    Luxembourg, Europe
    Well, if the equation has a predefined format like "a+b", you can simply use sscanf() to read the numbers just like you would from the shell. But if you're referring to reading numbers from any type of mathematical equation, you need to write a parser, which isn't very hard (for mathematical equations only) but probably too complicated for a beginner.

  4. #4
    Registered User
    Join Date
    Apr 2007
    still not understanding ....

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    As in

    char operator;
    int a,b;
    if ( sscanf( "12+13", "%d%c%d", &a, &operator, &b ) == 3 )
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Apr 2007
    You could tokenize the string.

    There's a C function that goes something like this:

    char *strtok( char *string1, const char *string2 );

    Where string11 is the string you wish to tokenize (in this case, "12+13") and string2 is what you use to break up the string (in this case, "+"). This will basically read the string until it finds the "+" sign, stop there, and return that string, which is thus "12". Once this function returns NULL, you've finished tokenizing the string.

    From there, you can use int atoi( const char *string ) to convert the resulting strings into integers.

    Just for reference, to do this with the number 12, it would look somewhat similar to this:

    number = atoi( strtok( string, "+" ) );

    Here's a small tutorial on C tokenizing here on this site:

    I'm going on the assumption, though, that you do not necessarilly need the "+" sign, which may not be the case. This is just to start you off, I suppose.

  7. #7
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Cambridge, UK
    char *strtok( char *string1, const char *string2 );
    Don't use strtokk, it alters your string. Either u will have keep a backup of your original string or use sscanf();


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to combine these working parts??
    By transgalactic2 in forum C Programming
    Replies: 0
    Last Post: 02-01-2009, 08:19 AM
  2. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  3. My graphics library
    By stupid_mutt in forum C Programming
    Replies: 3
    Last Post: 11-26-2001, 06:05 PM
  4. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 05:26 PM