-5

I need to split input string (a space delimited array of words), into a string array with maximum length M without breakdown any word. How can do this in C++?

For example:

std::string inputStr = "I need to split input string";
unsigned M = 10;
// split M characters by M characters
std::vector<std::string> output = split_string(inputStr, " ", M); 
//output contains {"I need to ","split ","input ","string"} 
cbuchart
  • 10,847
  • 9
  • 53
  • 93
dt128
  • 81
  • 9
  • 1
    I don't think there is something like this in the standard library. Did you try to implement it on your own? – Anedar Feb 17 '16 at 13:35
  • 1
    @dt128 As for me then I have not understood how you get the result array.:) – Vlad from Moscow Feb 17 '16 at 13:36
  • @Vlad from Moscow I mean vector of string no array – dt128 Feb 17 '16 at 13:50
  • You're saying three different things regarding that M length: **into string array with max length 'M' ** would mean M is the maximum number of elements in the resulting vector; the first comment indicates that you actually want to start splitting the original string starting at position M and ignore the beginning of the string; the output you're expecting suggests that M should actually be the maximum length of each resulting token. So what exactly are you trying to accomplish? – Ionut Feb 17 '16 at 14:05
  • @Ionut in fact it edited incorrectly,I edit it back, – dt128 Feb 17 '16 at 14:47

3 Answers3

0
using namespace std;

char str[] = "I need to split input string";
int M=10;

vector<string> data;
string part;

char* p = strtok(str, " ");
while (p != NULL) {
  string buf = part + string(p);
  if(buf.length() > M && !part.empty()) {
    data.push_back(part);
    part.clear();
  }
  part = string(p);
  p = strtok(NULL, " ");
}
if(!part.empty()) {
  data.push_back(part);
}
nariuji
  • 288
  • 1
  • 6
  • 1
    Why use C strtok() in an otherwise good looking piece of C++ code? There are pure C++ ways to split a string, see for example http://stackoverflow.com/questions/236129/split-a-string-in-c . – Ionut Feb 17 '16 at 14:23
  • @nariuji : don't work, just do word separation excepted first M characters – dt128 Feb 20 '16 at 13:00
  • I think anything is OK if it is easy. regex re(".{1,10}\b"); smatch m; regex_match(string(str), m, re); – nariuji Feb 20 '16 at 16:07
0

This code gives exactly the output you want, but it does a bit more than just "split by spaces".

http://ideone.com/mrIvLV

std::string str = "I need to split input string";   
std::vector<std::string> output;

std::istringstream iss(str);
std::string word;
const int max = 10;

while((iss >> word))
{   
    // Check if the last element can still hold another word (+ space)
    if (output.size() > 0 && (output[output.size() - 1].size() + word.size() + 1) <= max)
        output[output.size() - 1] += ' ' + word;
    else        
        output.push_back(word);
}
Jts
  • 3,447
  • 1
  • 11
  • 14
  • You have `std::` in `std::vector` but not with ``. – Aiman Al-Eryani Feb 17 '16 at 14:41
  • @José do work, but How to generalize this method for arbitrary delimiter ? – dt128 Feb 20 '16 at 12:36
  • You could change while((iss >> word)) to while(getline(iss,word,',')) [now the comma is the delimiter) for example. Accept my answer if it was what you were looking for :P – Jts Feb 20 '16 at 18:52
0
std::vector<std::string> split_string(const std::string &str, const std::string delim = " ", size_t pos = 0)
{
  std::vector<std::string> out;
  if (pos >= str.size())
    return out;

  size_t currentPos = 0;
  while (str.find(delim, pos + 1) != std::string::npos){
    out.push_back(str.substr(currentPos, str.find(delim, pos + 1)-currentPos));
    pos = str.find(delim, pos + 1);
    currentPos = pos;
  }
  out.push_back(str.substr(pos));

  return out;
}
Aiman Al-Eryani
  • 709
  • 4
  • 19