-5
#include<bits/stdc++.h>

using namespace std;

int main()
{        
    string s;

    cin>>s;        
    for(int i=0;i<(((int)s.length())-1);i++) // look here
    {
        if(s[i]==s[i+1])
        {    
            s.erase(i,2);
            i=-1;    
       }
    }
    if(s.length()==0)    
        cout<<"Empty String\n";
    else
    {
        cout<<s<<"\n";
    }
}

it gives me the right output.
But when I use this for-loop I was facing an issue. please explain what's the difference between these two. thanks in advance

    for(int i=0;i<s.length()-1;i++) // look here
   
  • 3
    `for(**int i=0;i – Tony Tannous Aug 09 '21 at 08:24
  • 1
    Formatting doesn't work in code blocks, better use a comment `//` to mark a line. Or your code won't compile. – Lukas-T Aug 09 '21 at 08:24
  • 1
    You seem to be discussing different output, one being correct. Then your explanation ends.... You need to fix that, because currently the answers and comments focus on the asymmetric `()` problem, which prevents the answer from providing help with your output problem. Make sure to provide two clean [mre] for comparison. – Yunnosch Aug 09 '21 at 08:27
  • 2
    Welcome to Stack Overflow. Please, format your entire code properly and remove unnecessary empty lines. Note that you are asking people here to help you for free, so don't waste their time. Pretty formatted code can be read and understood faster. – Daniel Langr Aug 09 '21 at 08:30
  • 1
    I took the liberty of assuming an attempt to highlight relevant lines and replace that with comments, to help you get helpful answers. Please check that it is what you want. – Yunnosch Aug 09 '21 at 08:31
  • 2
    Habits to drop: including bits header, using namespace std, inconsistent indentation, wasteful use of empty lines. – Yunnosch Aug 09 '21 at 08:37
  • By now you got an answer which answers what I think you want to ask - and helpfulyl as far as I can tell. Please still [edit] your question to really ask the right question (better fix the typos). Try for [ask]. Long term you want to improve the score even of your already answered questions. – Yunnosch Aug 09 '21 at 08:43
  • @Yunnosch: I'm super confused by your edit rejection `This edit did not correct critical issues with the post - view the revision history to see what should have been changed.` I simply fixed the terrible formatting ... and after your rejection you then edited it in a near-identical fashion. Looking at the history wasn't enlightening. What's the deal? – Elliott Aug 09 '21 at 10:31
  • Hi @Elliott You do well to ask me. I perceived the edit proposal which I overrode (wondering wether it could have been a different one than yours, because your name seems unfamiliar...) as only fixing a single typo outside of code. If my perception was wrong (possibly because I had an edit view which I normally do not use, or any little laziness on my part..) I apologise. If you tried or did fix all typos, improved readability and code formatting (which was the intend of my edit) then there was an accident which does not mean your were wrong. Sorry. – Yunnosch Aug 09 '21 at 10:39
  • @Elliott The given rejection reason by the way is automatic, I did not phrase it. – Yunnosch Aug 09 '21 at 10:45
  • @Yunnosch, No problem. Mistakes happen. – Elliott Aug 09 '21 at 10:54
  • See [Why should I not #include ?](https://stackoverflow.com/q/31816095) and [Why using namespace std is bad practice](https://stackoverflow.com/questions/1452721). – prapin Aug 09 '21 at 20:27

2 Answers2

4

Except for the typo the main difference is

s.length() - 1

vs.

((int)s.length()) - 1

The difference will only show if s is an empty string (thus s.length() == 0). Note that std::string::length() returns an unsigned integer.

So for the case where s is empty, s.length() - 1 will underflow and yield a huge number - the loop will run for a long time.

The second version will cast the value to int (a signed integer type) before substracting 1 - ((int)0) - 1 will yield the desired result -1.

Lukas-T
  • 11,133
  • 3
  • 20
  • 30
  • 1
    A short but explained answer (with a touch of clairvoyance on what is actually meant to be asked). Almost always gets my upvote. :-) – Yunnosch Aug 09 '21 at 08:38
  • But I guess it's the n-th dupe... ;) – Evg Aug 09 '21 at 08:42
  • @Evg Not unlikely. Find and propose one and I will join in dupe-voting. – Yunnosch Aug 09 '21 at 08:45
  • @Evg Yes, probably. If you find a good one I can retract the answer :) – Lukas-T Aug 09 '21 at 08:46
  • That's the problem - I spent some time trying to find a good dupe but didn't succeed. At the same time I can't believe there are no good dupes... Upvoted, anyway. – Evg Aug 09 '21 at 08:54
  • :-) Then let's let this stand. I think churill earned the upvotes. And closing the question would probably confuse the asker (new contributor) into not improving it. – Yunnosch Aug 09 '21 at 08:55
0

The second version will cast the value to int (a signed integer type) before substracting 1 - ((int)0) - 1 will yield the desired result -1

  • 5
    This is a very short and most importantly unexplained part of the existing older answer. Please make more obvious what your additional contribution is. Note that a different angle of explanation IS a contribution, but I do not see something like that here. – Yunnosch Aug 09 '21 at 09:57