Solved

SPLIT in VC

Posted on 2004-10-12
6
268 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Ready to get started with anonymous questions?

It's easy! Check out this step-by-step guide for asking an anonymous question on Experts Exchange.

Question has a verified solution.

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

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

623 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