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.)