Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Use the following space to write the code for a function called \"split\'. This

ID: 3851388 • Letter: U

Question

Use the following space to write the code for a function called "split'. This function will take 1 argument, a string called "line", and return a vector of strings called "strs". The function will split up the line into multiple strings, separated by commas. For example if split (''break, this, string'') were called it should return a vector containing 3 strings: "break" "this" string". Your code does not have to be perfect, but please try and keep it as close to c++ compile-able as possible. If you do not remember the function names for vectors or strings, please try and use function names as close as possible. Bonus point: If your code looks "perfect" (c++ compile-able) to me, I will give you an extra bonus point.

Explanation / Answer

vector<string> split(string line) {
   int len = line.length(), prev = 0, i, pos = 0, count = 0;
   for (i=0; i<len; i++)
       if (line[i] == ',')
           count ++;
   count ++; //words = #comma + 1
   vector<string> strs;

   for (i=0; i<len; i++)
       if (line[i] == ',') {
           strs.push_back(line.substr(prev, i));
           prev = i + 1;
       }
       strs.push_back(line.substr(prev, i));

}

I kept thisngs clean and simple. If you have any doubt, please comment below