Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

SPLIT in VC

Posted on 2004-10-12
6
Medium Priority
?
270 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 336 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 332 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 332 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

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

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

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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 additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

971 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