I am trying to write a function that takes a string, and splits every X number of characters:

```
std::vector<std::string> DIFSplitStringByNumber(std::string s, int l)
{
    const char *c = s.c_str();  
    char buffer[l];
    std::vector<std::string> entries;
    entries.reserve(int(s.length() / l) + 1);

    int d = 0;   
    for(int i = 0; i < s.length() - 1;)
    {
        if(d != l)
        {
            buffer[d] = c[i];
            d++;
            i++;
        }
        else
        {
            entries.push_back(std::string(buffer, l));

            //Clear array
            memset(buffer, 0, l);
            d = 0;
        }       
    }

    return entries;
}

```

For example, If I called `DIFSplitStringByNumber("hello!", 2)`, I should get a vector containing:

```
[0] he
[1] ll
[2] o!

```

However, it only seems to get the first two results (the vector size is 2), and when I do something like `DIFSplitStringByNumber("hello", 2)`, it crashes, presumably because its trying to access an array index that doesn't exist (it expects 6 characters, but there are only 5). Is there a simpler way to do this?