Using strtok() to separate data

This is a discussion on Using strtok() to separate data within the C++ Programming forums, part of the General Programming Boards category; Is it possible to use strtok() to separate data, eliminating all whitespace? I have data that is separated by spaces, ...

  1. #1
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640

    Using strtok() to separate data

    Is it possible to use strtok() to separate data, eliminating all whitespace? I have data that is separated by spaces, tabs, and occasionaly new lines. Is it possible to use strtok to take care of this? I've tried using strpbrk() but I don't think it works the same way as strtok, by being able to use NULL as the first parameter to continue on from where you left off.
    Last edited by neandrake; 09-29-2004 at 03:13 PM.
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,668
    Yes, you can do that
    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.

  3. #3
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640
    har har.
    thanks
    should've asked for an example.
    plz to show one
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

  4. #4
    Its not rocket science vasanth's Avatar
    Join Date
    Jan 2002
    Posts
    1,683
    Code:
    #include <stdio.h>
    #include <string.h>
    
    void function(char *ptr)
    {
    const char delim[]=" \n\r";
    ptr=strtok(ptr,delim);
    
    while(ptr!=NULL)
        {
         printf ("%s\n",ptr);
         ptr = strtok (NULL, delim);
    
        }
    
    }
    
    
    int main()
    {
     char ptr[]="How do you do";  
     function(ptr);   
    
        
    }

  5. #5
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    >>to use strtok() to separate data, eliminating all whitespace?
    If you're looking to break the string into separate tokens and copy them elsewhere in individual pieces, do a board search or google or cppreference on strtok().

    And if you're having trouble with cppreference, here's the exact spot
    Just Google It. √

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

  6. #6
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640
    Ok, thanks. I was using www.cplusplus.com library reference to look up strtok, and even at www.cppreference.com they don't show if it's clear that if you use multiple delimiters, that they don't have to be in order. Thanks all.
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

  7. #7
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    >>they don't show if it's clear that if you use multiple delimiters, that they don't have to be in order.

    Oh. Well, last I checked strtok() just stops when it finds any one of the delimiters you specified - it doesn't search for the entire string of delimiters.
    Just Google It. √

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

  8. #8
    Registered User
    Join Date
    Sep 2004
    Posts
    27
    Do an iteration through your string with two counter variables. One for write, one for read. When you hit a valid spot, move whatever is in read to write, and increment both. If you hit a space, newline, tab or null, move your read but not your write.

    This will overwrite all the spaces in the string with whatever comes after the spaces. So "Hello all" would become "Helloall". Just be sure to increment the write counter one more time and plop the null terminator at the end.

    After that, you can even use strlen() to figure out how long it is and then copy it into a brand new, properly sized array, if for some reason you wish to discard the old larger one.

  9. #9
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    >>Do an iteration through your string with two counter variables.
    Better yet, use two char*'s as the iterators to eliminate needless implicit pointer arithmetic But I think the point of the question was to 'separate data' while ignoring whitespace. Fusing the string into one glob would do the opposite.

    Although, neandrake, I would recommend that you use a stringstream rather than manually doing it with strtok().
    Code:
    #include <string>
    #include <sstream>
    
    std::string input;
    //get the input into 'input'... however you do it
    
    std::istringstream ss;
    ss.str(input);
    
    int a;
    int b;
    std::string c;
    double d;
    
    sstr >> a >> b >> c >> d;
    It's really convenient. Alternately, you could use a std::stringstream if you want to append data to the stream as well (for example using the << operator) for later reading... or if you're just trying to glue stuff together into a string, use a std:stringstream and use the << operator to write to the stream, then use ss.str() to retrieve the result.
    Just Google It. √

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

  10. #10
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640
    Oh, I'm sorry. I got it working using strtok() with multiple delimeters. It's been working for about 3 hours now =). thanks though
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 04-18-2008, 10:06 AM
  2. Help debugging my program
    By shoobsie in forum C Programming
    Replies: 4
    Last Post: 07-05-2005, 07:14 AM
  3. Replies: 4
    Last Post: 06-14-2005, 05:45 AM
  4. [question]Analyzing data in a two-dimensional array
    By burbose in forum C Programming
    Replies: 2
    Last Post: 06-13-2005, 07:31 AM
  5. How do I base size of arrays on annother number?
    By Dual-Catfish in forum C++ Programming
    Replies: 15
    Last Post: 09-25-2001, 01:31 PM

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