Solved

SPLIT in VC

Posted on 2004-10-12
6
257 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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

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…
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 …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

743 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now