Thread: Parsing input

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    14

    Parsing input

    i tried to come up with an execise that would challenge me and it worked out too good. what i came up with is to parse some input, but i made it hard to do right. the input is a string with a bunch of fields, but the fields can have spaces and two of them are optional, heres the setup
    lastname <firstname> date age <height> address hobby
    firstname is optional, so is height, date can have spaces or any character separating each number, address can be pretty much anything and hobby is a complete sentence of any length.

    what i've come up with so far is to read the entire string and then use sscanf on all of the fields up to address, i think i have it worked out okay but i cant figure out how to read the address and hobby separately without botching things up.

    am i on the right track or is this totally wrong ? and how can i read everything so that it can work with the optional fields as well?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    If address and hobby both contain spaces, then you're pretty well sunk at the moment

    Unless there is something unique at the end of all addresses, like a postal/zip code

    It would be easier to read the address and hobby on separate lines

  3. #3
    Registered User
    Join Date
    May 2002
    Posts
    14
    at first i was gonna have hobby in double quotes, but i decided not to because that would make everything way too easy. so theres no way?

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    How about a field seperator, a single character that's guaranteed not to be in the real data, just there purely to split up the fields.

    >lastname : <firstname> : date : age : <height> : address : hobby

    Now you just strtok your way through it. Nice and easy!
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    Registered User
    Join Date
    May 2002
    Posts
    14
    How about a field seperator, a single character that's guaranteed not to be in the real data, just there purely to split up the fields.
    thats a good idea, but since i want to challenge myself making it easy isnt what i want.

  6. #6
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by Cicero

    thats a good idea, but since i want to challenge myself making it easy isnt what i want.
    OK, how's this.... Same thing, you have the field seperators, but if you have an empty field, you don't get two seperators in a row, you only get the one. So, how do you know that a field is missing?

    For example:
    >lastname : firstname : date : age : address : hobby
    Note that height is missing, but firstname is present.

    Now the challenge. Devise method for storing the list of fields that are present and are missing. Store this information in the first few bytes of the line (before the lastname field), using as few bytes as possible (hint: use binary bits, with 1 meaning field present, 0 meaning field not present). Use this map the parse the data. Understand what I'm waffling about?
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  7. #7
    Registered User
    Join Date
    May 2002
    Posts
    50
    There was once the story of a man who was in a disastrous boating accident out in the middle of the ocean. He was a very religious man, and believed with all his heart that God would save him from a watery doom.

    After swimming in freezing water for 12 hours, a helicopter came along and the pilot shouted to him, "We heard about the accident and we have come to help you!"

    "No thanks!", the man shouted back, "God will save me!".

    So the helicopter left and the man remained in the water. Another 8 hours pass, and as the man is beginning to lose conciousness, he sees a motor boat speeding towards him on the horizon. As the boat reaches him, its operator yells:

    "Come now boy, we are here to help you! Let us take you home!"

    And again the man says "No thanks! I'm waiting for God to save me!" So the boat leaves.

    The man eventually dies of dehydration and hypothermia, and when he meets God in heaven, he asks: "Lord, why didn't you save me from the water?"

    God replies, "What do you think the helicopter and the boat were for!?!?!?!?!"


    Dude, sometimes it's stupid to do things the hard way. If there is a simple way to write an algorithm it is usually the best way.

  8. #8
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >Dude, sometimes it's stupid to do things the hard way.
    But not when you're trying to learn something new, extend your knowledge etc. If no-one challenged themselves, we wouldn't get anywhere.

    I understand what you mean, and I do agree that in real life situations, the easiest route is often the way to go, but it doesn't apply to this thread.

    For example, a lot of newbies are challenged with writing functions that tell you how long a string is, or how many words in a sentence. But they are not allowed to use string.h, because that would make life too easy, and their programs would be one line long.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  9. #9
    Registered User
    Join Date
    May 2002
    Posts
    14
    Now the challenge. Devise method for storing the list of fields that are present and are missing. Store this information in the first few bytes of the line (before the lastname field), using as few bytes as possible (hint: use binary bits, with 1 meaning field present, 0 meaning field not present). Use this map the parse the data. Understand what I'm waffling about?
    i can use an int as the first field, there are four different combinations so i can use the first two bits, if the bit is 0 then the optional field is not there, if its 1 then it is. so 01 means the weight is there but the firstname is not

  10. #10
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by Cicero

    i can use an int as the first field, there are four different combinations so i can use the first two bits, if the bit is 0 then the optional field is not there, if its 1 then it is. so 01 means the weight is there but the firstname is not
    Personally, I'd have a bit for each field, not just the ones that are optional. Use another method within the program to determine if a field being missing is actually allowed to be missing or not. That way, its safe for future changes: say one day you decide hobby is optional, you'd only need to update a flag somewhere, without the need to muck about with adding extra bits.

    It's up to you, hopefully now you're on the road......
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  11. #11
    Registered User stautze's Avatar
    Join Date
    Apr 2002
    Posts
    195
    >thats a good idea, but since i want to challenge myself making it easy isnt what i want.

    Or maybe its a homework assignment?
    'During my service in the United States Congress, I took the initiative in creating the Internet.' - Al Gore, March 9, 1999: On CNN's Late Edition

  12. #12
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >so theres no way?
    Nothing that is guaranteed to work under all conditions.

    >Or maybe its a homework assignment?
    I doubt it, homework assignments usually have a simple enough answer so that rookies can solve it in a relatively short amount of time. If it is homework them he/she doesn't understand the requirements.

    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. About aes
    By gumit in forum C Programming
    Replies: 13
    Last Post: 10-24-2006, 03:42 PM
  2. Structure and Linked List User Input Question
    By kevndale79 in forum C Programming
    Replies: 16
    Last Post: 10-05-2006, 11:09 AM
  3. Need help fixing bugs in data parsing program
    By daluu in forum C Programming
    Replies: 8
    Last Post: 03-27-2003, 06:02 PM
  4. need help with some input
    By blindleaf in forum C Programming
    Replies: 2
    Last Post: 03-16-2003, 01:50 PM