The Python split() allows you to specify a string as the delimiter (since Python does not distinguish a separate character type from the string type; '.' is a string of length 1 rather than a character, and 'hi mom'[0][0][0][0][0][0][0][0] will not raise an exception), and
is case-sensitive (so you shouldn't be doing anything with tolower().
Also, the Python split function does not add quotes to the substrings. That's part of Python's built-in formatting for
displaying a representation of strings. You should definitely not add them in the C++ version.
Finally, there are a bunch of 'find' functions in the std::string interface that you seem to be unaware of. Don't make life hard for yourself.
Quote:Also in C++ is it possible to return an array from a function?
You can wrap an array in a structure and return an instance of the structure. Or you can use a pre-made structure for that purpose: boost::array (which is designed to let you treat it just like an array, with [] subscripting and everything).
However, for the current task, an array is inappropriate because you do not know ahead of time how many substrings there will be.
Here's what I came up with:
vector<string> split(const string& source, const string& delimiter, int limit = -1) { string::size_type position = 0; const int delimiter_size = delimiter.size(); if (delimiter_size == 0) { throw invalid_argument("empty delimiter"); } vector<string> result; // Note the use of '!=' here rather than '<' which allows us to treat a -1 // limit value as "infinity". The loop will still break when the string can't // be found any more. for (int i = 0; i != limit; ++i) { string::size_type found_at = source.find(delimiter, position); if (found_at == string::npos) { break; } result.push_back(string(source, position, found_at - position)); position = found_at + delimiter_size; } result.push_back(string(source, position)); return result;}