1

The following code is part of my project in C++98, therefore i'm not allowed to use vectors and such. Now the main use for this function is to break down a single string line, to an array of strings using the given delimeter, and the size is basically the number of words that i need to return. the problem is that when i debug and check nums towards the end, it changed it's size to 4, and only returned the first word, filled by every char of it.As if, nums is now char* i have changed the code many times, but i don't where i went wrong, any advice?

string* Split(string ss,char delimeter,int size)
{
    string *nums=new string[size];
    int index_c, index_sw=0;
    for (int i = 0; i < size; i++)
    {
        for(unsigned int j=0;j<ss.length();j++)
        {
            if (ss.at(j) == delimeter)
            {
                index_c = j;
                nums[i] = ss.substr(index_sw, index_c);
                index_sw += index_c;
                i++;
            }
        }
        break;
    }
    return nums;
}
8
  • 18
    C++98 had vectors. Commented Dec 11, 2018 at 15:49
  • nums is a pointer to the first element in the array. The debugger doesn't know how many elements it contains and can only safely assume it has 1. Commented Dec 11, 2018 at 15:49
  • 4
    Your logic looks flawed here...your inner loop loops over the whole string and splits things up nicely, but you put every chunk into the same index of the array. Then you increment which index you'll be placing into and loop the whole string doing it all again. Commented Dec 11, 2018 at 15:50
  • 1
    I don't understand the reason for the outer loop or how you know size. Commented Dec 11, 2018 at 15:52
  • 1
    your premise is wrong. use std::vector, though that might not solve all the issues with your code Commented Dec 11, 2018 at 15:59

1 Answer 1

1

Since you do not know the number of words in the string ss beforehand, you cannot specify size when calling the Split function. Not knowing size you will not be able to allocate memory to nums.

So you are better off using a vector of strings. As pointed out, vector is available in C++98.

Then your modified Split function will look like this:

vector<string> Split(string ss, char delimiter)
{
    vector<string> vs;
    int index_c, index_sw=0, j;

    for(j=0;j<ss.length();j++)
    {
        if (ss.at(j) == delimiter)
        {
            index_c = j;
            vs.push_back(ss.substr(index_sw, index_c - index_sw));                
            index_sw = index_c + 1;
        }
    }
    vs.push_back(ss.substr(index_sw, j - index_sw));        
    return vs;
}

which then can be called like this:

vector<string> ret = Split("This is a stackoverflow answer", ' ');

See demo here.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.