'I have a task (codewars): "Convert a string to an array" - Write a function to split a string and convert it into an array of words. For example:

My code:

#include <vector>
#include <string>
#include <cstring>
using namespace std;

  vector<string> string_to_array(const string& s) {
  int size = s.length();
  int num_space = 0;
  char space[] = " ";
  for(int i =0; i<size; i++)
    {
      if (s[i] == space[0])
        {
        num_space ++;
      }
    }
  int stop = 0;
  string ok = "\0"; 
  int l = 0;
  vector <string> result;
  for(int i =0; i<size; i++)
    {
    if (s[i] == space[0])
        {
        for (; stop<i; stop++){
          ok = ok + s[i];
        }
      result[l] = *ok.c_str();
      l++;
      ok = "\0";
      }
  }
  return result;
  }

error: Exit Code: 132 I just-just can't return "char" from a vector function. and came up with this error, what's wrong? to be sure: "Robin Singh" ==> {"Robin", "Singh"}

"I love arrays" ==> {"I", "love", "arrays"}



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source