Thread: Help me with split function.

  1. #1
    Banned
    Join Date
    Mar 2008
    Posts
    78

    Help me with split function.

    I have an CHAR array, in that array, each "place" has this syntax: "xxx-yyy"

    Where XXX is a Id value and YYY is a property.

    I want to make a function that goes to a "place" in that array, for example array[2], and receives something like this "437-662", and splits it into 2 int variables, the result may be like this:

    CHAR id = 437 and CHAR prop = 662.

    How can i do that?

    (no, i cant use struct or something....)
    Last edited by Milhas; 03-29-2008 at 07:21 AM.

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    You can use modulus division to separate everything less than 1000 and > 1000.

    int n = 437662 ;
    int prop = n % 1000 ;
    int id = n - prop ;
    Mainframe assembler programmer by trade. C coder when I can.

  3. #3
    Banned
    Join Date
    Mar 2008
    Posts
    78
    Quote Originally Posted by Todd Burch View Post
    You can use modulus division to separate everything less than 1000 and > 1000.

    int n = 437662 ;
    int prop = n % 1000 ;
    int id = n - prop ;
    hmmm very interesting solution.

    thanks,


    may i have more possible solutions, i'l learning C, so the more the better i learn

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    you can use strchr to find a '-' location and strncpy to copy part of the string
    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

  5. #5
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    You could convert the int to a 6 byte string, then memcpy() the first 3 characters into a new array, then strcat() a '-', then strcat() the last 3 characters.

    Todd
    Mainframe assembler programmer by trade. C coder when I can.

  6. #6
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Quote Originally Posted by vart View Post
    you can use strchr to find a '-' location and strncpy to copy part of the string
    The OP said he had an int array, but used an example of a string to show what he was storing. So, either he doesn't have an int array and is using a char array, in which your solution will work, or, he does have an int array and the values are encoded like I proposed.

    Todd
    Mainframe assembler programmer by trade. C coder when I can.

  7. #7
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    The OP said he had an int array
    What I see - It is the talking about CHAR array
    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

  8. #8
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    He (she) edited it. It used to say int array, and the variables were all ints.

    And for not apologizing for to me for me wasting my time, or posting to say he was mistaken when editing it, I banish him (her) to the no-response-from-me-for-now-on-queue.
    Mainframe assembler programmer by trade. C coder when I can.

  9. #9
    Banned
    Join Date
    Mar 2008
    Posts
    78
    Quote Originally Posted by Todd Burch View Post
    He (she) edited it. It used to say int array, and the variables were all ints.

    And for not apologizing for to me for me wasting my time, or posting to say he was mistaken when editing it, I banish him (her) to the no-response-from-me-for-now-on-queue.
    sorry man, but when i edited it was because i though that it was wrong. At the begining i was doing with INT, and now i'm still using INT, I was going to use char after all but after i saw your post, that you made some seconds before i edit the post, i leave has it was, in INT.

    But after that i let the edited post with CHAR because i wanted to see how i could do it with chars to so you'r post wasn't a waste of time.

    Thanks everyone!

  10. #10
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Thank you.
    Mainframe assembler programmer by trade. C coder when I can.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. doubt in c parser coding
    By akshara.sinha in forum C Programming
    Replies: 4
    Last Post: 12-23-2007, 01:49 PM
  3. Troubleshooting Input Function
    By SiliconHobo in forum C Programming
    Replies: 14
    Last Post: 12-05-2007, 07:18 AM
  4. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  5. Split function
    By fkheng in forum C Programming
    Replies: 7
    Last Post: 07-08-2003, 08:26 PM