How to use strtok?

This is a discussion on How to use strtok? within the C++ Programming forums, part of the General Programming Boards category; I have to write a program that inputs a phone number in this form (555) 555-5555. I have to use ...

  1. #1
    Registered User
    Join Date
    Dec 2002
    Posts
    8

    How to use strtok?

    I have to write a program that inputs a phone number in this form (555) 555-5555. I have to use the strtok function, but what is the point? im not sure. It mentions that the area code string should be converted into int and convert phone number string to long. I dont even know what this problem is asking for. Any help with this will be appreciated it. Thanx

  2. #2
    Registered User
    Join Date
    Aug 2002
    Posts
    170
    strtok is used to parse strings. Like in your phone number example you can use the parenthesis as a marker that seperates the data you actually want. You call strtok and pass it the string you want to deal with and the seperator you want to use. You can call it multiple times to get the next field. In which case you would use a NULL as the string, and you can change the delimiter each time as needed.


    Actual doc:
    The strtok subroutine breaks the string pointed to by the String1 parameter into
    a sequence of tokens, each of which is delimited by a byte from the string
    pointed to by the String2 parameter. The first call in the sequence takes the
    String1 parameter as its first argument and is followed by calls that take a
    null pointer as their first argument. The separator string pointed to by the
    String2 parameter may be different from call to call.

    --Bonkey
    Last edited by bonkey; 12-16-2002 at 11:43 AM.

  3. #3
    fuh
    fuh is offline
    Attack hamster fuh's Avatar
    Join Date
    Dec 2002
    Posts
    176
    Sounds like homework...
    Stupid things pop singers say

    "I get to go to lots of overseas places, like Canada."
    - Britney Spears

    "I love what you've done with the place!"
    -Jessica Simpson upon meeting the Secretary of Interior during tour of the White House

  4. #4
    Just a Member ammar's Avatar
    Join Date
    Jun 2002
    Posts
    953
    Originally posted by fuh
    Sounds like homework...
    I think this is a good way to ask for help, he is not asking for the whole homework, he wants to know to use strtok().

    Well...
    To tokenize a string you have to make sequential calls to strtok(), untill you get NULL in the string returned by strtok()...
    char *strtok( char *string1, const char *s2);
    where string1 is the string to want to tokenize.
    String2 is are the character that the tokinizing is based on.

    I'll show you how does it work by an example:
    Code:
    char []string="bla bla bla";
    char *tokenPtr;
    
    tokenPtr = strtok( string, " ");
    
    while ( tokenPtr != NULL )
    {
         cout << tokenPtr << '\n';
         tokenPtr = strtok( NULL, " ");
    }
    none...

  5. #5
    Shadow12345
    Guest
    and seeing as how no one seems to have mentioned it already, go to msdn.microsoft.com if you know the name of a function but want to know how to use it. Just type in the name of the function and blamo lots o info comes up! It extends across many apis including opengl, very useful stuff!

  6. #6
    Registered User
    Join Date
    Jan 2003
    Posts
    648
    Even the "I'm Feeling Lucky Button" in Google works for this:
    http://www.cplusplus.com/ref/cstring/strtok.html

    Remember that the dilimeter string can have more than one character. For example, your dilimeter string could be " ()-".

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,551
    Bit late - I mean, the thread was over a year old before being bumped
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  8. #8
    Registered User
    Join Date
    Jan 2003
    Posts
    648
    How did it end up the in the "New Posts"? That's all I ever check. Sorry anyways.

  9. #9
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    and seeing as how no one seems to have mentioned it already, go to msdn.microsoft.com if you know the name of a function but want to know how to use it.
    Been there, done that, for this exact function. Took one look at the docs, and looked for an alternate solution (and I never did find out how the function worked until today) Personally, I find that the docs are often overly technically-precisely worded, making the meaning of the whole thing obscure and hard to grasp.

    **EDIT**
    DOH, even that post was a couple years old...
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  10. #10
    Registered User
    Join Date
    Oct 2002
    Posts
    69
    so if you use a delimiter string such as "()-", will it token the string based on each individual character from that string, or will it look for that string to tokenize?

    like if i wanted to token '-', '|', '/', and ' ', i would just make the delimiter string "-|/ "?

  11. #11
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Quote Originally Posted by talz13
    so if you use a delimiter string such as "()-", will it token the string based on each individual character from that string, or will it look for that string to tokenize?

    like if i wanted to token '-', '|', '/', and ' ', i would just make the delimiter string "-|/ "?
    Try it and see.
    My best code is written with the delete key.

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, 04:48 PM
  2. strtok is causing segmentation fault
    By yougene in forum C Programming
    Replies: 11
    Last Post: 03-08-2008, 09: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

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