Solved

SPLIT in VC

Posted on 2004-10-12
6
264 Views
Last Modified: 2010-04-01
Which code in VC will be the best replacement for the Splict function in VB. Please provide a code which can be used anywhere in VC
0
Comment
Question by:Cateran
6 Comments
 
LVL 12

Accepted Solution

by:
OnegaZhang earned 84 total points
ID: 12295027
/* strtok example */
#include <stdio.h>
#include <string.h>

int main ()
{
  char str[] ="This is a sample string,just testing.";
  char * pch;
  printf ("Splitting string \"%s\" in tokens:\n",str);
  pch = strtok (str," ");
  while (pch != NULL)
  {
    printf ("%s\n",pch);
    pch = strtok (NULL, " ,.");
  }
  return 0;
}

Welcome to www.fruitfruit.com
0
 
LVL 39

Assisted Solution

by:itsmeandnobodyelse
itsmeandnobodyelse earned 83 total points
ID: 12296317
int split(const string& s, vector<string>& vs, char sep = ' ')
{
      string::size_t pos  = 0;
      string::size_t lpos = 0;
      int count = 0;
      while ((pos = s.find(sep, lpos)) != string::npos)
      {
              if (pos - lpos > 0)
              {
                  vs.push_back(s.substr(lpos, pos - lpos));
                  count++;
              }
              lpos = pos + 1;              
      }
      if (lpos < s.length())
      {
           vs.push_back(s.substr(lpos));
           count++;
      }
      return count;
}

Regards, Alex
0
 
LVL 17

Assisted Solution

by:rstaveley
rstaveley earned 83 total points
ID: 12296321
If you want to embrace C++ rather than going for C, you should use std::string for your strings rather than character arrays.

Here's one person's implementation of a C++-ish split using std::string:

http://www.mindcracker.com/mindcracker/c_cafe/stl/PartingSTLStrings.asp
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question