Thread: strtok nightmare

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    36

    strtok nightmare

    I am having serious problems attempting to use strtok, I've been staring at the following for hours and cannot figure out why null is showing up everywhere.

    I'm using strtok to parse out all words from a string using a space as the delimitor. The first iteration works perfectly, the second iteration and those that follow always have (null) after the first word is parsed.

    The second iteration and those that follow should have more than one word, for some reason, only the first word is present from the second iteration and those that follow.

    I've attached a screenshot of my output.

    http://www.wjdev.net/problem.JPG

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <ctype.h>
    
    void encode(char msg[], int key);
    void reverse(char msg[]);
    
    int main()
    {
    
        int iteration = 0;
        char code[] = ".kmpeqgf dmqz szupzq ziafziap zu ezaufmgfue kozqsdqyq fdabqd qouxaB .pqfoqbege eU zaufmgomhq kmitsuT .kdqbbuxe kxszuiazw faz eu kmitsut qtF .femq szupmqT kxxmuoqbeq eqdufiaze qeg pzm egaufgmo kxqyqdfjq qN .eziaf zdqfemq edqtfaye zaufmfubuoqdb pqfoqbjqzG .iaze szuemqdozU :ftsuzaf dqtfmqi ftsuq eiqZ";
        char *p = NULL;
        char delimitor[] = " ";
        char *target = "and";
        int found = 0;
    
        /* reverse the string */
        reverse(code);
        
        do
        {
    
            iteration++;
            encode(code, iteration);
            
            p = strtok(code, delimitor);
            printf("&#37;s\t%i\n", p, iteration); //test
            
            /* while token isn't equal to NULL */
            while( p != NULL )
            {
                /* compare token for instance of "and" */
                if(strcmp(p, target)==0)
                {
                    /* if found then break 
                    out of loop */
                    found = 1;
                    break;
                }
    
                p = strtok( NULL , delimitor );
                printf("%s\t%i\n", p, iteration);
    
                if( ( p == NULL ) && ( iteration == 2 ) ) // PROBLEM
                {
                    printf("\n\n%s\n\n", code);
                }
            }
    
            if (found == 1)
                break;
    
        }
        while(iteration < 25);
    
        printf("Key Code is %i", iteration);
        
        return 0;
        
    }
    
    void encode(char msg[], int key)
    {
         int i;
         for ( i = 0; i < strlen(msg); i++ )
         {
             if ( islower(msg[i]) ) msg[i] = ( ( ( msg[i] - 97 ) + key ) % 26 ) + 97;
             if ( isupper(msg[i]) ) msg[i] = ( ( ( msg[i] - 65 ) + key ) % 26 ) + 65;
         }
    }
    
    void reverse(char msg[])
    {
         int c, i, j;
         for ( i = 0, j = strlen(msg) - 1; i < j; i++, j-- )
         {
             c = msg[i];
             msg[i] = msg[j];
             msg[j] = c;
         }
    }
    Last edited by Cero.Uno; 04-19-2008 at 09:26 PM.

  2. #2
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    If you check the man page for strtok, you'll see that strtok modifies the tokenized string, indeed replacing the delimiter with a null character. Make a copy to tokenize if you want to reuse the original later.

  3. #3
    Registered User
    Join Date
    Feb 2008
    Posts
    36
    dude, you are a legend, thank you.

    Code:
    char data[strlen(code)];
    
    /* copy tokenise since strtok 
    alters original string */
    strcpy(data, code);

  4. #4
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    Quote Originally Posted by Cero.Uno View Post
    Code:
    char data[strlen(code)];
    
    /* copy tokenise since strtok 
    alters original string */
    strcpy(data, code);
    strlen(code) is not long enough to copy the string; the string contains a null character which strlen() does not count. The length should be strlen(code) + 1.

  5. #5
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Or better yet, do it at compile time (Which is C89 standards compliant),

    Code:
    char data[sizeof(code)];
    However, Personally I tend to avoid using variable size stack variables like that.

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. Help debugging my program
    By shoobsie in forum C Programming
    Replies: 4
    Last Post: 07-05-2005, 07:14 AM
  5. Trouble with strtok()
    By BianConiglio in forum C Programming
    Replies: 2
    Last Post: 05-08-2004, 06:56 PM