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
Solved

SPLIT in VC

Posted on 2004-10-12
6
265 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 Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
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 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.

765 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