0

My question is this, when the inner for loop exits, and enters back into the outer for loop it stops adding characters to the string pointer pstrDestination. Can some one explain that to me, I am not terminating the array of character so it should still write, shouldn't it?

// Does it match
if (strcmp(strCompareString, pstrToFind) == 0)
{

    // Reset the index of the found letter
    intFoundLetterIndex = (intFoundLetterIndex - intCompareIndex);

    // Add the characters from source to destination.
    for (intSourceIndex = 0; intSourceIndex < intSourceLength; intSourceIndex += 1)
    {

        pstrDestination[intDestinationIndex] = pstrSource[intSourceIndex];
        intDestinationIndex += 1;

        // Are we at the beginning of the target word
        if (intSourceIndex == intFoundLetterIndex)
        {

            // Add the replacement characters to the destination.
            for (intNewIndex = 0; intNewIndex < intReplaceWithLength; intNewIndex += 1)
            {

                pstrDestination[intDestinationIndex - 1] = pstrReplaceWith[intNewIndex];
                intDestinationIndex += 1;

            }

            intSourceIndex += intToFindLength;
        }

    }

}
11
  • I don't get what you're asking. Did you step through your code using the debugger already? Commented Sep 8, 2015 at 19:03
  • You should learn about ++ and -- for incrementing and decrementing variables. Commented Sep 8, 2015 at 19:05
  • 1
    intDestinationIndex - 1; What do you think this statement actually does? Commented Sep 8, 2015 at 19:06
  • yes, Okay I am replacing a word in an string, but instead of using the built in string Data Type I am using an Array of Characters. When I reach the point where the word I want to replace begins, I replace it with a new word or phrase. but when the replacement is done, and I exit out of the inner for loop. The outer for loop chugs along like it should but it stops adding characters to pstrDestination, its like it is terminated, but I dont terminate the string. Commented Sep 8, 2015 at 19:06
  • 1
    The continue statement is redundant. Commented Sep 8, 2015 at 19:09

2 Answers 2

2

I think this

intDestinationIndex - 1;

should look like this:

intDestinationIndex -= 1;
Sign up to request clarification or add additional context in comments.

Comments

0

Best I can come up with is, The Visual Studio 2013 IDE is trying to give me a huge big ol' hug. It is terminating the string for me. if I step the index back 1 and set the spot in the array equal to ' '. Then the loop executes as expected cause I over wrote the terminator.

after the inner loop I added;

pstrDestination[intDestinationIndex - 1] = ' ';

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.