Loading ints specified in a string into an array

This is a discussion on Loading ints specified in a string into an array within the C Programming forums, part of the General Programming Boards category; I'm doing an assignment for a C course I am in. For part of the assignment I am loading integers ...

  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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,424
    strtol will convert string to integr and provide pointer where the conversion was stopped, so you can continue parsing string from that point
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

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, 02:40 AM
  3. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 10: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, 09:33 PM

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