user3417930
user3417930

Reputation: 13

Odd result when calling method more than once

I have created a class that is supposed to behave like std::string in a way. All of the functions that I have tested seem to work fine, but I am creating a justify method which is supposed to add spaces to a string in the already existing blank spots. The method is relatively simple:

String String::justify(const int Width) const
{
     String result = *this;
     int i = 0;

     if(nextBlank(0) == -1 || length() > Width)
          return *this;

      while(result.length() < Width)
      {
           i = result.nextBlank(i); 
           if(i == -1) result.nextBlank(0); 
           result = result.substr(0,i) + ' ' + result.substr(i + 1, result.length());
           i = result.nextNonBlank(i);
      }
      return result;
}

When this method is called I usually get a strange blend of chars because I think it's going past the Null terminating char somewhere, but I don't know where or why and that's what I'm hoping you guys could help me with. I believe it has to do with the substr method because that is when it outputs strangely. Even when not in the justify function if substr is called twice the output will be incorrect, but when called once it will work fine.

Also, here is the nextNonBlank, nextBlank, and substr functions because they are relevant:

int String::nextBlank(const int Index) const 
{
    int i = Index;

    while(i <= length()) 
    {
        if(S[i] == ' ' && (i != Index || S[0] == ' ')) 
        {
            return i;
        }

        else if(i == length())
            return -1;

        ++i;
    }   

    return -1;
}       


int String::nextNonBlank(const int Index) const 
{
    int i = Index;

    while(i <= length()) 
    {
        if(S[i] != ' ' && i != Index  )
        {
            return i;
        }

        ++i;
    }   

    return -1;
}


String String::substr(int Start, int End) const 
{
    String result;
    assert(End <= length());

    for(int i = Start; i <= End; ++i)
        result = result + S[i];

    return result;
}

The entire code is posted here if you guys think it might be a problem with the constructors or something else: http://pastebin.com/mTakMPtq

If anybody could take the time to look at this I would appreciate it immensely. I have been stuck on this for longer than I'd like to admit.

Here is an example test:

int main()
{
    String str("Here is a string.");

    std::cout << "Without justify:" << std::endl;
        str = str.substr(0, 3) + " " + str.substr(3 + 1, str.length()); 
    std::cout << str << "\n\n"; 

    std::cout << "With justify:" << std::endl; 
    std::cout << str.justify(25);
}

And here is the output:

Without justify: ÐC*Here is a string.

With justify: ÐC*ÐC*Here ÐC* is a string.

Upvotes: 1

Views: 98

Answers (1)

trm
trm

Reputation: 420

Something is wrong with your substr() function. Added some variables for debugging and ran with 2 different strings. Here is the first iteration for each:

    while(result.length() < Width)
    {
            i = result.nextBlank(i);
            String temp1 = result.substr(0,i);
            String temp2 = " ";
            String temp3 = result.substr(i + 1, result.length());
            int tmpint = result.length();

            result = result.substr(0,i) + " " +result.substr(i + 1, result.length());
            i = result.nextNonBlank(i);
    }
i: 1
tmpint: 34
result: 0x603450 "A string without a beginning space"
tmp1: 0x6038b0 "    with beginning space\021\001A "
tmp2: 0x603670 " "
tmp3: 0x603ad0 "x¦[÷ÿ\177string without a beginning space"

i: 0
tmpint: 24
result: 0x603450 "    with beginning space"
tmp1: 0x6038b0 "x¦[÷ÿ\177x¦[÷ÿ\177x¦[÷ÿ\177x¦[÷ÿ\177\021\001 "
tmp2: 0x603690 " "
tmp3: 0x603ad0 "   with beginning space"

Upvotes: 1

Related Questions