# while loop which doesn't end

This topic is 3382 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi Guys,

I've been stuck on this problem for the past 2 nights. The actual puzzle gets deeper than this but I can't for the life of me figure out why this code doesn't work. Basically the loop doesn't end. Hopefully the code/comments is self explanatory. Help please!

PS: Forgive me but I used 'plus' instead of the symbol because for some reason that gets filtered out.

string x = "hellohellohowdoyoudo";
int counter = 0;
int position = 0;

// Count the number of times "hello" occurs in given string.
// Loop ends if position is -1 (string function find() returns -1 when number of instances is 0).

while(position!=-1)
{
position = x.find("hello", position);
position plus=1;
counter plus= 1;
}

cout << counter;

##### Share on other sites
string x = "hellohellohowdoyoudo";int counter = 0;int position = 0;// Count the number of times "hello" occurs in given string.// Loop ends if position is -1 (string function find() returns -1 when number of instances is 0).while(position!=-1){position = x.find("hello", position);position plus=1;counter plus= 1;}cout << counter;

Some of the logic is a bit off with your approach. Consider the case of the word "hello" not even being present in the word you're looking at. The variable "position" would be set to -1, then set to 0 (1 added to it) and then the counter would increase. Position would not trigger the exit condition of the loop (since it is 0 again) and the same sequence of events would happen.

Now consider the search word you have setup already with 'x' ("hellohellohowdoyoudo").

-Loop does not fail (position is equal to 0)
-position is set to 0, substring of "hello" found at position 0
-position is now 1
-counter is now 1
-loop does not fail (position is equal to 1)
-position is set to 5, substring of "hello" found at position 5
-position is now 6
-counter is now 2
-loop does not fail (position is equal to 6)
-position is set to -1, no substring of "hello" found
-position is set to 0
-counter is now 3
....
loop will continue from here on out.

From here, or in a situation like this, I would just walk though your debugger to see what is happening to the variables on each line. Hopefully this helps out a bit.

[Edited by - Skaff on July 15, 2010 5:57:25 PM]

##### Share on other sites
Suppose that find() returns -1. What happens between then, and the next time the while-condition is checked?

##### Share on other sites
Assuming you're using std::string, then std::string.find() returns std::string.npos, not -1 when it can't find anything. Of course, Sneftel's point is very true!

##### Share on other sites
Quote:
 Original post by SneftelSuppose that find() returns -1. What happens between then, and the next time the while-condition is checked?

Aha the -1 is incremented by 1. But I was under the impression that the while loop ends as soon as the value 'position' becomes -1. Ok I'll try and recode that.

By the way this isn't my 'homework'. I wish it was and I were a student again but no, this is from a practice puzzle I found on topcoder. Dithering problem!

##### Share on other sites
Quote:
 Original post by EzbezAssuming you're using std::string, then std::string.find() returns std::string.npos, not -1 when it can't find anything. Of course, Sneftel's point is very true!

Ah true, I forgot that npos was returned instead of -1. Has been a bit since I worked with the std:string! I was going by the comments placed in.

##### Share on other sites
Quote:
 Original post by Skaff *** Source Snippet Removed ***Some of the logic is a bit off with your approach. Consider the case of the word "hello" not even being present in the word you're looking at. The variable "position" would be set to -1, then set to 0 (1 added to it) and then the counter would increase. Position would not trigger the exit condition of the loop (since it is 0 again) and the same sequence of events would happen. Now consider the search word you have setup already with 'x' ("hellohellohowdoyoudo").-Loop does not fail (position is equal to 0)-position is set to 0, substring of "hello" found at position 0-position is now 1-counter is now 1-loop does not fail (position is equal to 1)-position is set to 5, substring of "hello" found at position 5-position is now 6-counter is now 2-loop does not fail (position is equal to 6)-position is set to -1, no substring of "hello" found-position is set to 0-counter is now 3....loop will continue from here on out.From here, or in a situation like this, I would just walk though your debugger to see what is happening to the variables on each line. Hopefully this helps out a bit.

That does help. I think where I went wrong is that I forgot that a while loop tests a condition at the beginning of the loop.

I've modified the code by including an if statement, which exits the loop as soon as position equals to -1. It works but it seems inefficient because I still have the condition for the while loop. Everything works as I want now, just optimizing it!

##### Share on other sites
Quote:
 Original post by spaceJockey123I've modified the code by including an if statement, which exits the loop as soon as position equals to -1. It works but it seems inefficient because I still have the condition for the while loop. Everything works as I want now, just optimizing it!

Or you could move the position = x.find() to immediately before the while loop, and copy it to the end of the while loop:

position = x.find("hello", position);while (position != -1) {  position plus= 1;  counter plus= 1;  position = x.find("hello", position);}

##### Share on other sites
Maybe..
count = position = 0;while( position < x.Length() && (position=x.find(...)) != npos ){   position++;   count++;}

Haven't tried it but you may not need "position < length" if you never search for a 1 character string instead of >1 char string.

[Edited by - Buckeye on July 15, 2010 10:48:41 PM]

• ### Game Developer Survey

We are looking for qualified game developers to participate in a 10-minute online survey. Qualified participants will be offered a \$15 incentive for your time and insights. Click here to start!

• 15
• 21
• 23
• 11
• 25