grabbing an int from a string

This is a discussion on grabbing an int from a string within the C Programming forums, part of the General Programming Boards category; now i have a problem if there is an equation like 12+13 in a string , how can i grab ...

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    14

    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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    strtol for example
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    Lean Mean Coding Machine KONI's Avatar
    Join Date
    Mar 2007
    Location
    Luxembourg, Europe
    Posts
    444
    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
    Posts
    14
    still not understanding ....

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,592
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Registered User
    Join Date
    Apr 2007
    Posts
    12
    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:
    http://faq.cprogramming.com/cgi-bin/...&id=1044780608

    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
    Location
    Cambridge, UK
    Posts
    1,682
    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();

    ssharish2005

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, 07: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, 05:05 PM
  4. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 04:26 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21