previous | start | next

Splitting Example

...
#include <cstring>

int main()
{
  string line;
  cout << "\nEnter a line with comma separated values" << endl;
  getline(cin, line);
  const char *s = line.c_str();
  char *p = new char[strlen(s) + 1];
  strcpy(p, s);
  char *q;

  q = strtok(p, ";");
  while( q != NULL ) {
    cout << q << endl;
    q = strtok(NULL, ";");
  }

  return 0;
}
Input Line: Jason R Bloch;5512 Garden Lakes Dr.;Flushing, NY 01234

Output:
Jason R Bloch
5512 Garden Lakes Dr.
Flushing, NY 01234



previous | start | next