Balanced Parantheses

Hello sir/ma’am.
My code for balanced parentheses always shows “yes”, i tried a lot to debug it. But could not find the error. could you please tell me where i am going wrong. sharing my code:

suppose input is ::
a+(b*©%d}(
then the answer should be “no” but it is coming “yes”. In my code, i am trying to deal with all the other possible chars as well, not only the opening or closing brackets.

Hey @priyanshi.agarwal3405

  1. You need to remove the statement
    if(str[i]!=’(’ || str[i]!=’)’ || str[i]!=’[’ || str[i]!=’]’ || str[i]!=’{’ || str[i]!=’}’)
    continue;
    from the beginning of your for loop. Because of this statement, if any open or closed bracket appears, continue statement runs, and none of the opening brackets is pushed on stack, i.e lower statements are not executed. So remove the above statement from your code.

  2. Also, you need to remove the statement
    if(s.empty())
    return false;
    because it is invalid here.

The corrected code is:
https://ide.codingblocks.com/s/167686

In my code, the lines which you are telling to remove are dealing with all the other chars other than opening and closing brackets like “a,b,%,+…” etc etc. and that just increases the value of i in loop. in line no. 14 and 15 i have dealt with opening brackets also. So i am unable to understand how the removal of lines 12 and 13 is affecting the push of opening brackets. Could you please elaborate.

if(s.empty()) return false; also these lines are there to deal with the case that before any opening bracket , directly a closing bracket is coming. in that case, i think its important to return false straight away…

Could you please reply???

Hello Hello Hello ??

Please reply. It’s been a week since i have asked this doubt, still it’s not been cleared. could you please reply…

Sorry @priyanshi.agarwal3405 for late reply.
You say that:

“if(s.empty()) return false; also these lines are there to deal with the case that before any opening bracket , directly a closing bracket is coming.”
So you should return false in this case when closing bracket comes. So first you have to check whether the current character is a closing bracket, if its a closing bracket and s.empty() is true then it should return false, right! So now in the corrected code sent by me:

if(str[i]==’)’)
{
x=s.top();
s.pop();
if(x==’(’)
continue;
return false;

    }

See here whenever a closing bracket appears, if s.top() is the required matching character it’ll continue and move on to the next character. But if this if condition is not satisfied , then it directly moves to the next statement which says return false, so if there’s no required matching opening bracket or the stack is empty when the closing bracket comes, this code returns false, so handled this thing in this code, and you dont need to write the lines you’re saying.

Also,
According to line no 12 and 13,
if(str[i]!=’(’ || str[i]!=’)’ || str[i]!=’[’ || str[i]!=’]’ || str[i]!=’{’ || str[i]!=’}’)
continue;
Whenever any opening or closing bracket comes, you execute continue statement, which means that move to the next element in the string. It basically increases the value of i by 1 in the for loop. So accoding to this statement, whenever any opening or closing bracket comes, you do nothing with it, and just move to the next character, which is wrong, coz you’ve to do operations with the brackets.
(Kindly read how continue statement works to have a clear understanding of it.)

In line 12 and 13 , i have used not equal to “!=” any brackets.

so in line 12 and 13 we’ll do continue only to avoid any chars other than the brackets

could you reply please…

@priyanshi.agarwal3405
okay right, my bad, didn’t notice, you can keep line no 12 & 13, they’re correct.

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.