Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

C beginner

Options
  • 25-04-2005 11:03pm
    #1
    Closed Accounts Posts: 139 ✭✭


    Just getting back into c coding after a long time (and wasn't very good to begin with).

    What I need to do is take in a text document (50 or 60 words) and parse it so that each individual word is sepperated from one another.

    Using strtok I have been take words from the original file and write them individually to another file. i.e:

    "Due to the recent increase in online auction fraud it is now
    mandatory for you to verify your account "
    Written to new file:
    Due
    to
    the
    recent
    increase
    in
    online
    auction
    fraud
    it
    is
    now etc.

    what I need to do is put each of these words into an element of an array so that each word can be analysed individually. Can this be done using structs or am I missing something very obvious.
    Any help would be greatly appreciated
    Regards
    John


Comments

  • Closed Accounts Posts: 17,208 ✭✭✭✭aidan_walsh


    char str[] = "Due to the recent increase in online auction fraud it is now mandatory for you to verify your account ";
       char delims[] = " ";
       char *result = NULL;
       result = strtok( str, delims );
       while( result != NULL ) {
           printf( "result is \"%s\"\n", result ); //you won't need this bit, only to verify that its working - but you knew that :)
           result = strtok( NULL, delims );
       }
    

    Adapted from http://www.cplusplus.com/ref/cstring/strtok.html, so appologies if it doesn't work. I don't know C\C++, so I was just working with what I saw.


  • Registered Users Posts: 950 ✭✭✭jessy


    Ya that should work fine, i would make one change though, if the delimiter is only a space then you don’t need an array just a point to a char.


  • Closed Accounts Posts: 139 ✭✭john_g83


    thanks for the reply guys. Unfortunately I don't think the method mentioned by doodle will work as the code needs to be adaptable to work on different text segments/text files. Had a brainstorm in bed last night that I could analyise each word as it is tokenised and then write to a new file (basically any word > 6 or 7 letters will be replaced by a synonym to modify the text file). Jessy am using all types of punctuation as delimeters as I need ti identify individual words.
    Will try the above and let y'all know how I get on. Thanks again for the help
    John.


  • Registered Users Posts: 4,003 ✭✭✭rsynnott


    The way strtok works is by replacing the tokensing character with '\0'. So you should be able to do strtok and then make each item it returns an element of an array of pointers. That what you're looking for?


  • Closed Accounts Posts: 139 ✭✭john_g83


    rsynnott wrote:
    The way strtok works is by replacing the tokensing character with '\0'. So you should be able to do strtok and then make each item it returns an element of an array of pointers. That what you're looking for?
    yea, that sounds kind of like what I was thinking of. any further suggestions


  • Advertisement
Advertisement