Thread: Loading ints specified in a string into an array

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    1

    Loading ints specified in a string into an array

    I'm doing an assignment for a C course I am in.

    For part of the assignment I am loading integers that are specified in a string of characters into an array.

    For example, if I have this string:
    "1,5-7,12"
    I want to load the numbers 1, 5, 6, 7, 12 into an array of integers.

    I could probably do this with a large amount of code, but I am wondering if there is some function that will help me out a bit here. I figure sscanf isn't going to do much for me.

    The main problem I see is with multi-digit integers, like 12. In the string it is stored as the two seperate characters '1' and '2', I would need to convert that into the integer 12.

    I would really appreciate it if someone could give me an idea of the best way to go about this... thank you.

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    strtol will convert string to integr and provide pointer where the conversion was stopped, so you can continue parsing string from that point
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 09:11 AM
  2. Custom String class gives problem with another prog.
    By I BLcK I in forum C++ Programming
    Replies: 1
    Last Post: 12-18-2006, 03:40 AM
  3. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  4. can't assign proper values to an array of string
    By Duo in forum C Programming
    Replies: 1
    Last Post: 04-04-2005, 06:30 AM
  5. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 10:33 PM