1

I'm really new to coding and I'm having some trouble with trying to split strings in C++. I'd like to know how to split a string, which is input as a const char names[] (i.e. "Mary, Jan, Jane") in C++ without using any external libraries (i.e. I don't want to have to use #include <string> etc. - although I can use #include <cstring>).

I've tried using:

const char names[] = "Mary, Jan, Jane";

char *token = strtok(names, ",");
while (token != NULL) {
    token = strtok(NULL, " ");
}

But I can't seem to pass in a const array of chars, and I'd also like to know then how you would access all the individual "tokens"?

ALSO I've tried changing the input to just char names[] (but I do need the input to be const), and I get a segmentation error and I don't understand why.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
J Dope
  • 51
  • 2
  • 4

4 Answers4

5

Use std::string instead of char arrays and utilize the std::stringstream class. Pass the , delimiter to a std::getline function:

#include <iostream>
#include <string>
#include <sstream>

int main() {
    std::string names = "Mary, Jan, Jane";
    std::string temp;
    std::istringstream ss(names);
    while (std::getline(ss, temp, ',')) {
        std::cout << temp << '\n';
    }
}

The only thing left is to handle the leading space character in each of the strings:

if (temp.front() == ' ') {
    temp.erase(0, 1);
}
Ron
  • 14,674
  • 4
  • 34
  • 47
1

strtok modifies the input string. The string is passed as a char*, not a const char*. It replaces the delimiters by '\0'. This makes it incompatible with a string literal, which is a const char*.

I would never use this function, because it also maintains an internal state, which makes it unuseable with multiple threads or even calling other functions that also use strtok while you are in a function that uses strtok.

If you want to see some (many, actually) ways to split a string in C++, take a look at this Q/A:
The most elegant way to iterate the words of a string

alain
  • 11,939
  • 2
  • 31
  • 51
1

The correct usage of strtok() is as follows:

int main ()
    {
      char str[] ="Mary, Jan, Jane";
      char * pch;
      pch = strtok (str," ,");
      while (pch != NULL)
      {
        printf ("%s\n",pch);
        pch = strtok (NULL, " , ");
      }
      return 0;
    }
0

My preferred method is to use boost::algorithm::split

http://www.boost.org/doc/libs/1_63_0/doc/html/string_algo/usage.html#idp11326066

It is safer then strtok, which is a C function expecting c-strings.

It will require you use std::string and a container such as std::vector.

You mention you don't want to use

#include <string>

I'd question: why not? If you are going to be programming in C++, being familiar with std::string and the STL is going to be essential.

If you are new to coding, you might not be familiar with boost yet, but it is one of the most commonly used libraries in C++ there is. In fact, alot of what goes into the standard comes from boost.

Christopher Pisz
  • 3,757
  • 4
  • 29
  • 65