Thread: token

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    80

    token

    is there any function in c++ which is the same as strtok() function in c,if there is can u give a simple example

  2. #2
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    strtok() can be used in c++ the same way its used in c
    just include <cstring>

  3. #3
    Registered User Cela's Avatar
    Join Date
    Jan 2003
    Posts
    362
    If you mean with C style strings then you can just use strtok in <cstring>, if you mean with the string class then you can do something like this
    Code:
    #include <iostream>
    #include <string>
    
    string bad_char = " ";
    
    string next_token(string& s, string::size_type& pos)
    {
      size_t begin = s.find_first_not_of(bad_char, pos);
      size_t end = s.find_first_of(bad_char, begin);
    
      pos = end;
    
      if (begin == string::npos)
      {
        return string();
      }
      else if (end == string::npos)
      {
        return s.substr(begin);
      }
      else
      {
        return s.substr (begin, end - begin);
      }
    }
    
    int main()
    {
      string::size_type pos = 0;
      string s = "Breaking up a string";
      string t;
    
      while (!(t = next_token(s, pos)).empty())
      {
        cout<< t <<endl;
      }
    }
    *Cela*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Connecting to a mysql server and querying problem
    By Diod in forum C++ Programming
    Replies: 8
    Last Post: 02-13-2006, 10:33 AM
  2. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  5. Parsing and Tokens (strtok)
    By readerwhiz in forum C Programming
    Replies: 6
    Last Post: 04-22-2002, 09:57 AM