1

My while loop has become an infinite one inspite of using decrement operator. Could you please explain why is that so? Isn't it supposed to come out of the while loop once the condition becomes false?

    # include<bits/stdc++.h> 
    using namespace std; 

    const int MAX_CHAR = 26; 

    // Function to print the string 
    void printGrouped(string str) 
    { 
        int n = str.length(); 

        // Initialize counts of all characters as 0 
        int  count[MAX_CHAR] = {0};           

        for (int i = 0 ; i < n ; i++) 
            count[str[i]-'a']++; 

        for (int i = 0; i < n ; i++) 
        {                
            while (count[str[i]-'a']--)
                cout << str[i];
        } 
    } 

    // Driver code 
    int main() 
    { 
        string str = "applepp";           
        printGrouped(str); 
        return 0; 
    } 
Christophe
  • 68,716
  • 7
  • 72
  • 138
bunny bugs
  • 35
  • 7
  • 1
    When you visit a letter, you set it's count to -1. You decrement until it's value used to be 0 so the `while` loop always leaves the counter at -1. The next time you visit it, it's value is not zero and decrements forever. Just put the decrementing of `count` inside the loop. – François Andrieux Jan 10 '20 at 20:07
  • ... or change your condition to `while (count[str[i]-'a']-- > 0)`. Also you may be interested in [this post](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h) explaining why including bits/stdc++ is a bad practice. – luciole75w Jan 10 '20 at 20:22
  • @François Andrieux thank you so much! – bunny bugs Jan 10 '20 at 21:11
  • @luciole75w thank youu! oh alright would include the headers separately from now on,thanks – bunny bugs Jan 10 '20 at 21:17
  • **Recommended reading:** [Why should I not #include ?](https://stackoverflow.com/q/31816095/560648) – Lightness Races in Orbit Jan 11 '20 at 21:09

2 Answers2

1

What I can see is that you have an expression in your while loop's "()" brackets. It's better to have a condition over there you should put this expression inside the loop and then you need to add a condition related to that expression in these brackets "()" let's suppose you wish to exit the loop when the expression's value is -1. I am not sure about the synatx, Just tried to demonstrate my logic in the code.

  x= count[str[i]-'a']-1;
   While(x!=-1)
   {
     cout << str[i];
     x= count[str[i]-'a']-1;
   }
Rabia Kanwal
  • 191
  • 1
  • 12
1

The problem is

while(count[str[i]-'a']--) { ... }

the reason is the expression

x--

decrements x and returns the original value (before decrementing). Using a while condition like

while(x--) { ... }

quits the loop when x goes from 1 to 0, but if you enter the while again then you've a problem because x made it to -1 and it will not get back to zero by decrementing it.

-1 is a "true value" for a while test, so it will enter the loop and become -2, then loop again and become -3 and so on until you get an overflow and undefined behavior.

The loop should be probably written as

while(count[str[i]-'a']) {
    count[str[i]-'a']--;
    ....
}

so that you decrement it ONLY if it's not already zero

6502
  • 112,025
  • 15
  • 165
  • 265