Thread: Reading integer from string

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    2

    Reading integer from string

    Hello

    I have a string named "test" with "(1,2)"

    How do I extract "1" and "2" from the string and put them as integers?

    I tried

    int l1, l2;
    ...
    sscanf(teste,"s%d", &l1);

    to try to extract the first number but it doesn’t work.


    Thanks in advance!

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    scanf() and sscanf() do pattern matching so you can try this... but no promises...
    Code:
    int l1, l2;
    ...
    sscanf(teste,"(%d,%d)", &l1,&l2);
    And just a couple of pointers...
    1) please post code in code tags... click the octothorpe (#) on the editor toolbar and put your code between the resulting tags.
    2) I'm aware this is just a quicky example but I think I'd pick better variable names for the real thing...

    Hope it works for you....

  3. #3
    Registered User
    Join Date
    Apr 2011
    Posts
    2
    Quote Originally Posted by CommonTater View Post
    scanf() and sscanf() do pattern matching so you can try this... but no promises...
    Code:
    int l1, l2;
    ...
    sscanf(teste,"(%d,%d)", &l1,&l2);
    And just a couple of pointers...
    1) please post code in code tags... click the octothorpe (#) on the editor toolbar and put your code between the resulting tags.
    2) I'm aware this is just a quicky example but I think I'd pick better variable names for the real thing...

    Hope it works for you....

    Thank you for the answer

    It's Working, thank you very much!
    Last edited by dr_kaufman; 04-30-2011 at 10:05 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading an integer array from console??
    By pico in forum C Programming
    Replies: 16
    Last Post: 12-23-2008, 08:53 AM
  2. Reading char as integer.
    By esben in forum C Programming
    Replies: 15
    Last Post: 05-04-2006, 01:52 PM
  3. Reading integer from file.
    By esben in forum C Programming
    Replies: 4
    Last Post: 03-04-2006, 12:39 PM
  4. reading long hexadecimal integer
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 05-27-2002, 01:31 AM
  5. Reading input as an integer
    By n0de in forum C++ Programming
    Replies: 4
    Last Post: 04-11-2002, 06:52 PM