Thread: strtok problem

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

    strtok problem

    /
    PHP Code:
    Complete the program to count the number of words in the sentence an$
            
    // print each word separately, along with length of each word.

            // If line = "Arizona is hot", your output should be
            // Word 1: Arizona      Length = 7
            // Word 2: is           Length = 2
            // Word 3: hot          Length = 3
    char *ptr;

            
    ptrstrtok(line,",");
    printf("word:%s  length:%d \n\n",ptr,strlen(ptr));
            
    ptr=strtok(NULL,",");
    printf("word:%s length:%d \n\n",ptr,strlen(ptr)); 
    for the second time i use a strtok i get a segmentation fault why is that?

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    what is line?

  3. #3
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Compare ptr to NULL after you call strtok(). It's possibly failing because there's nothing left after you look for the first ','.

  4. #4
    Registered User
    Join Date
    Feb 2008
    Posts
    28
    Code:
    #include <stdio.h>
    #include <string.h>
    
    // This function takes a character and a string
    // as input and computes if the character is in
    // the string or not.  Returns 1 if yes, 0 otherwise.
    int  is_char_in_string(char c, char *s)
    {
            while (*s != '\0')
            {
                    if (c == *s) return 1;
                    s++;
            }
            return 0;
    }
    main()
    {
            char line[255];
            char delim[10] = " .,;!?";     // set of delimiting characters that can$
            int i;
            int length;
    
            // Get a line from user as input
            printf("Enter a sentence: ");
            gets(line);
    
            // The line before we replace the delimiting characters with 0.
            length = strlen(line);
            for(i=0;i<length; ++i)
            {
                    printf ("%3c ", line[i]);
            }
            printf("\n\n");
    
    
     length = strlen(line);
            //Replace delimiting characters with '\0'.
            for(i=0;i<length; ++i)
            {
                    if (is_char_in_string(line[i], delim))
                            line[i] = '\0';
            }
     // The line after we replace delimiting characters with 0.
            for(i=0;i<length; ++i)
            {
                    printf ("%3c ", line[i]);
            }
            printf("\n\n");
            // Printing the line as integers so that we can see that space is
            // replaced with 0.
            for(i=0;i<length; ++i)
            {
                    printf ("%3d ", line[i]);
            }
       printf("\n\n");
    
    
            // Complete the program to count the number of words in the sentence an$
            // print each word separately, along with length of each word.
    
            // If line = "Arizona is hot", your output should be
            // Word 1: Arizona      Length = 7
            // Word 2: is           Length = 2
            // Word 3: hot          Length = 3
    
    char *ptr;
    
            ptr= strtok(line,",");
            if(ptr != NULL)
    printf("word:%s  length:%d \n\n",ptr,strlen(ptr));
    
            ptr= strtok(' ',",");
    printf("word:%s  length:%d \n\n",ptr,strlen(ptr));
    
            
    }[
    here is the rest of the code what do you guys think is wrong.

  5. #5
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Same thing I said was wrong last time.

  6. #6
    Registered User
    Join Date
    Feb 2008
    Posts
    28
    do you mean
    strcmp(ptr,NULL)

  7. #7
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Code:
    ptr= strtok(' ',",");
    Add a check for NULL after this....

    And change it back to:

    Code:
    ptr= strtok(NULL,",");

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #9
    Registered User
    Join Date
    Feb 2008
    Posts
    28
    PHP Code:
       ptrstrtok(NULL,",");
            if(
    ptr != NULL)
    printf("word:%s  length:%d \n\n",ptr,strlen(ptr));

            
    ptrstrtok(NULL,",");
            if(
    ptr != NULL)
    printf("word:%s  length:%d \n\n",ptr,strlen(ptr));


    }
                              


    Enter a sentencearizona is hot
      a   r   i   z   o   n   a       i   s       h   o   t

      a   r   i   z   o   n   a      i   s      h   o   t

     97 114 105 122 111 110  97   0 105 115   0 104 111 116

    word
    :arizona  length:
    why is it not printing the other stuff, i thought for ther second time i call the strtok it starts at "is".

  10. #10
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    you have put \0 instead of space - for any standard C-string function it means - string ended here... so your string contains only "arizona" nothing more could be found using stndard C-functions...
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  11. #11
    Registered User
    Join Date
    Feb 2008
    Posts
    28
    so how can i fix that?

  12. #12
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by rambos View Post
    so how can i fix that?
    1. do not put \0 into your string before calling strtok
    2. write your own function that knows how to handle the string with \0 inside
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  13. #13
    Registered User
    Join Date
    Feb 2008
    Posts
    28
    is there a way to undo the \0 other than just erasing the code from before? in other words but it back as one of the deliminating characters.

  14. #14
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    is there a way to undo the \0 other than just erasing the code from before? in other words but it back as one of the deliminating characters.
    yes... you have original length of the string - walk along and replace \0 with char of your choice...
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  15. #15
    Registered User
    Join Date
    Feb 2008
    Posts
    28
    Code:
    PHP Code:
    for (line[i] ='\0') {  line[i]= delim[0]; } 
    i wrote this before your last post in hopes that it would change them to space but only gave me a bunch of errors when i compiled it.

    word-count.c: In function `main':
    word-count.c:71: error: syntax error before ')' token
    word-count.c: At top level:
    word-count.c:79: error: conflicting types for 'ptr'
    word-count.c:77: error: previous declaration of 'ptr' was here
    word-count.c:79: error: `line' undeclared here (not in a function)
    word-count.c:79: warning: initialization makes integer from pointer without a cast
    word-count.c:79: error: initializer element is not constant
    word-count.c:79: warning: data definition has no type or storage class
    word-count.c:81: error: syntax error before string constant
    word-count.c:83: error: redefinition of 'ptr'
    word-count.c:79: error: previous definition of 'ptr' was here
    word-count.c:83: error: redefinition of 'ptr'
    word-count.c:79: error: previous definition of 'ptr' was here
    word-count.c:83: warning: initialization makes integer from pointer without a cast
    word-count.c:83: error: initializer element is not constant
    word-count.c:83: warning: data definition has no type or storage class
    word-count.c:84: error: syntax error before "if"
    word-count.c:87: error: redefinition of 'ptr'
    word-count.c:83: error: previous definition of 'ptr' was here
    word-count.c:87: error: redefinition of 'ptr'
    word-count.c:79: error: previous definition of 'ptr' was here
    word-count.c:87: warning: initialization makes integer from pointer without a cast
    word-count.c:87: error: initializer element is not constant
    word-count.c:87: warning: data definition has no type or storage class
    word-count.c:88: error: syntax error before "if"
    word-count.c:91: error: redefinition of 'ptr'
    word-count.c:87: error: previous definition of 'ptr' was here
    word-count.c:91: error: redefinition of 'ptr'
    word-count.c:79: error: previous definition of 'ptr' was here
    word-count.c:91: warning: initialization makes integer from pointer without a cast
    word-count.c:91: error: initializer element is not constant
    word-count.c:91: warning: data definition has no type or storage class
    word-count.c:92: error: syntax error before "if"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  2. Replies: 6
    Last Post: 04-28-2006, 12:06 PM
  3. Strange problem - strtok
    By AngKar in forum C Programming
    Replies: 7
    Last Post: 04-23-2006, 07:36 PM
  4. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM
  5. Replies: 5
    Last Post: 11-07-2005, 11:34 PM