Thread: what does strtok return?

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    15

    what does strtok return?

    What does strtok return when it finds "nothing" between two delimiters? (It seams that it is not NULL).

    I need to insert a character between two deliniters, but cannot find any if-condition for this case.

    Thank you.

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    if strtok() finds nothing that generally means that the 2 delim chars occupy consecutive memory locations. Read up to first delim and place in an array or something. Then strcat() what you want onto the end. Then finally strcat() again onto that the end of the original string. Always make sure your destination array is big enough to cope with the 2 strcat operations without overflowing.
    Simple to do with the funcs in string.h and/or pointers.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  3. #3
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    strtok() should return NULL if it does not find the key in a string.

    Kuphryn

  4. #4
    Registered User
    Join Date
    Sep 2002
    Posts
    15
    This is what I already know :
    strtok() returns a pointer to a string that includes the delimiter AND replaces the delimiter by NULL.
    That means: in case there are two delimiters in a row (like ;;) strtok() should return NULL. In fact it only does so at the input-string's end!
    Code:
    /* strtok example */
    #include <stdio.h>
    #include <string.h>
    
    int main ()
    {
      char str[] ="S1;;;;0;;1;;;";
      char * pch;
      cout << "Splitting string in tokens:" << endl;
      pch = strtok (str,";");
      while (pch != NULL)
      {
        cout << pch << " ";
        pch = strtok (NULL, ";");
      }
      return 0;
    }
    The Output looks like this: S1 0 1
    If strtok() had returned NULL when it found two ';' in a row, "0 1" would be missing.

    I need to fill in 0 between each ';'.
    The output has to look like: S1 0 0 0 0 0 1 0 0

    @ Stoned_Coder:
    Surely I can append any string at EACH token I get with strcat(). But how do I append only at the tokens with "nothing" in it? I only see that my output will look like this which is not what I want:
    S10 0 0 0 00 0 10 0 0
    Did I get it wrong?

    Thank You.
    Last edited by Zwen; 09-29-2002 at 12:40 PM.

  5. #5
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    This is just one of the many reasons I have strtok(). However, as much as I hate strtok() you can work with it. You should always do some sort of preprocessing of text that you are tokenizing. You should count the number of delimeters and get take care of crap like two consecutive delimeters.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How can I make this code more elegant?
    By ejohns85 in forum C++ Programming
    Replies: 3
    Last Post: 04-02-2009, 08:55 AM
  2. sort linked list using BST
    By Micko in forum C Programming
    Replies: 8
    Last Post: 10-04-2004, 02:04 PM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  4. opengl program as win API menu item
    By SAMSAM in forum Game Programming
    Replies: 1
    Last Post: 03-03-2003, 07:48 PM
  5. opengl code not working
    By Unregistered in forum Windows Programming
    Replies: 4
    Last Post: 02-14-2002, 10:01 PM