1
string *parse(string str,int from){
int i=0,n=0,j,k;
i=j=from;
string *data=new string[6];
while(str[i]){
    if(str[i]==' '){
        for(k=0;k<(i-j-1);k++){
            data[n][k]=str[j+k]; << Error takes place here
        }
        data[n][k]='\0';
        j=i;
        n++;
    }
    i++;
}
return data;
}

Thanks for your help. I tried to debug but without success, what am I missing?

4
  • What is the exact error message? Commented Sep 7, 2013 at 0:13
  • data starts with 0 length, data[n][k] out of boundry Commented Sep 7, 2013 at 0:13
  • Are you trying to split string by space? Commented Sep 7, 2013 at 0:21
  • yes im trying to split by space. Commented Sep 7, 2013 at 0:24

2 Answers 2

2

The problem is that elements data[i] of the data array all have the length of zero. That is why the assignment data[n][k] is always outside of data[n]'s range.

One way of fixing this would be using concatenation:

data[n] += str[j+k];

A better approach would be eliminating the loop altogether, and using substr member function of std::string instead: it lets you cut out a portion of str knowing the desired length and the starting position.

In addition, you are returning a pointer to a local array, which is undefined behavior. You should replace an array with a vector<string>, and add items to it using push_back.

Finally, you need to push the final word when the str does not end in a space.

Here is your modified program that uses the above suggestions:

vector<string> parse(string str,int from){
    int i=from, j=from;
    vector<string> data;
    while(str[i]){
        if(str[i]==' '){
            data.push_back(str.substr(j, i-j+1));
            j=i+1;
        }
        i++;
    }
    if (j != str.size()) {
        data.push_back(str.substr(j));
    }
    return data;
}

Here is a demo on ideone.

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

2 Comments

Might have been worth doing some of the legwork with string::find
@kfsone You are right, there are ways to optimize this code for better use of the library. I wanted to keep as much of OP's code structure as possible, so I kept the loop the way it was.
1

data starts with 0 length, data[n][k] out of boundry. data[n][k]='\0' is not correct way of using C++ string and string * is considered of bad practice.

To separate a string by space, try:

  #include <string>
  #include <vector>
  #include <sstream>

  std::string data("hi hi hi hi hi");
  std::stringstream ss(data);
  std::string word;

  std::vector<std::string> v;
  while(std::getline(ss, word, ' '))
  {
    v.push_back(word);
  }

2 Comments

I dont want to use other libraries
@MikeL that's not other libraries, it's standard C++ library. The way you use like C operation loses the point of using string

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.