Thread: strtok with addition

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    61

    strtok with addition

    Hi all!

    I have a large string built up of many words, that I want to cut into many smaller strings, one to each word. For this purpose I'm using strtok. My problem is that after some words there is commas and dots, that I don't want to have in the cut strings. What function should I use? Perhaps memset?

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Why not strtok? The seperator argument can be longer than one character.

  3. #3
    Registered User
    Join Date
    Sep 2009
    Posts
    61
    but the point is that there is not always a dot or a comma after the word, so I want to use many possibilities for the separator argument.

    I don't know why this compiles, but it doesn't seem to work. Though perhaps it can inspiring.
    Code:
    strtok(NULL, " " "\n" ", " ". ");

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    It doesn't matter.

    Quote Originally Posted by strtok man page
    char *
    strtok(char *restrict str, const char *restrict sep);

    ...

    The strtok() function is used to isolate sequential tokens in a null-ter-
    minated string, str. These tokens are separated in the string by at
    least one of the characters in sep.
    strtok(NULL , "\n., "); works just as well if the string is delimited by all periods as if it's delimited by random parts of sep.
    Last edited by whiteflags; 12-27-2009 at 10:21 AM.

  5. #5
    Registered User
    Join Date
    Sep 2009
    Posts
    61
    Wow! That looks cool. I didn't know that. Now it works. Thanks for the help!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 20q game problems
    By Nexus-ZERO in forum C Programming
    Replies: 24
    Last Post: 12-17-2008, 05:48 PM
  2. strtok is causing segmentation fault
    By yougene in forum C Programming
    Replies: 11
    Last Post: 03-08-2008, 10:32 AM
  3. trying to use strtok() function to parse CL
    By ohaqqi in forum C Programming
    Replies: 15
    Last Post: 07-01-2007, 09:38 PM
  4. converting string to integer, for further use
    By shoobsie in forum C Programming
    Replies: 2
    Last Post: 07-01-2005, 03:12 AM
  5. Trouble with strtok()
    By BianConiglio in forum C Programming
    Replies: 2
    Last Post: 05-08-2004, 06:56 PM