strtok function

This is a discussion on strtok function within the C Programming forums, part of the General Programming Boards category; Here is the sample code: ================================================== ======== Code: /* strtok example */ #include <stdio.h> #include <string.h> int main () { ...

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    10

    strtok function

    Here is the sample code:
    ================================================== ========
    Code:
    /* strtok example */
    #include <stdio.h>
    #include <string.h>
    
    int main ()
    {
      char str[] ="- This, a sample string.";
      char * pch;
      printf ("Splitting string \"%s\" into tokens:\n",str);
      pch = strtok (str," ,.-");
      while (pch != NULL)
      {
        printf ("%s\n",pch);
        pch = strtok (NULL, " ,.-");
      }
      return 0;
    }
    ================================================== =========

    I have not understood the the following line in above piece of code:

    " pch = strtok (str," ,.-");"

    What's going on inside the parenthesis within the double quotes?

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Read the man page.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Jan 2011
    Posts
    10
    Quote Originally Posted by quzah View Post
    Read the man page.

    Quzah.
    Could anyone explain how my code is performing as per man page mentioned in above reply?

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Did you actually read the man page, or are you just expecting someone to answer because you're too lazy to read it? Why should I expect you to read what I type here, if you didn't read what I typed here?


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Omnipotent View Post
    Could anyone explain how my code is performing as per man page mentioned in above reply?
    Although it pains me deeply I'm going to agree with Quzah on this one...

    There are three primiary skills a programmer needs..
    1) The ability to look things up when needed.
    2) The patience to learn and keep up to date.
    3) Strong trouble shooting ability.

    Notice that none of those say anything about writing code...

    So I will give you to things to think about...

    1) How on earth did you ever write code you don't understand?
    2) Why can't you follow the documentation for your code?

    (And, in case you are wondering... Yes this is the kind of crappy situation scoop and poop coding gets you into)

  6. #6
    Registered User
    Join Date
    Jan 2011
    Posts
    10
    Quote Originally Posted by CommonTater View Post
    Although it pains me deeply I'm going to agree with Quzah on this one...

    There are three primiary skills a programmer needs..
    1) The ability to look things up when needed.
    2) The patience to learn and keep up to date.
    3) Strong trouble shooting ability.

    Notice that none of those say anything about writing code...

    So I will give you to things to think about...

    1) How on earth did you ever write code you don't understand?
    2) Why can't you follow the documentation for your code?

    (And, in case you are wondering... Yes this is the kind of crappy situation scoop and poop coding gets you into)
    Thank you for asking so many questions.
    That's not my code.I was trying to understand the usage of strtok().I'm a beginner hence was expecting some explanation from your side.

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You should quit whatever it is you're doing, because clearly you are completely beyond helping yourself and have no basic reading comprehension. Tell your teacher that you are too stupid to program and ask them to fail you right now. That way you will stop wasting everyone's time, and stop taking up a seat in a class that someone else could be using who actually wants to learn.

    All you had to do is read the link I provided, and you would have been given a fine explanation of exactly what it is doing, including example code for you to look at.


    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Next time say it's not your code... really... 99% of the stuff we get here is put forth as people's own code so there's a ccertain assumption.

    However something around 50% of it is probably scoop and poop code being passed off as a person's own code... so when someone puts up a piece of code then asks how it works, I'm guessing you can figure what the natural assumption is.


    Now if you were to say "I found this code fragment and I'm trying to figure out how it works..."
    (or something very similar) it abates the suspicion and gets you a lot better response.

    To answer your question, the second part of strtok() is a search list. Strtok locates the beginning of a token by finding the first character that is NOT in the list. This becomes the pointer it returns. Then it carries on and looks for characters that are in the list to locate the end of the token, which it replaces with a NULL. When the function returns you have a pointer that can be treated like a string.

    (FWIW... that much is explained in most man pages and help files)....

    Next time, actually read the links provided.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sudoku Brute Force Algorithm Help (recursion?)
    By ridilla in forum C Programming
    Replies: 22
    Last Post: 05-07-2010, 04:31 PM
  2. dllimport function not allowed
    By steve1_rm in forum C++ Programming
    Replies: 5
    Last Post: 03-11-2008, 04:33 AM
  3. doubt in c parser coding
    By akshara.sinha in forum C Programming
    Replies: 4
    Last Post: 12-23-2007, 01:49 PM
  4. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  5. Question..
    By pode in forum Windows Programming
    Replies: 12
    Last Post: 12-19-2004, 07:05 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21