here i am implementing this for practice not for the assignment
Why i am not getting correct output for the input
Hello @Deepanshu_garg,
The very first thing that I noticed about your code:
-
It will print multiple lines of output.
Example:
(())
Expected output:
Balanced
Your Output:
Balanced
Balanced
Balanced -
All other issues revolve around the above mentioned logical error.
Example:
(()}
Your Output:
Balanced
Not balanced
Expected Output:
Not balanced
So, the final output of your program is correct, but it is printing few extra lines. -
Your code will produce runtime error for testcases like:
())
Expected Output:
Not balanced
Your Output:
run-time error
Reason:
when you code would check for third character ‘)’(i.e. the second closing bracket in the input string), the stack would be empty, thus the following conditional expression would cause an error.
else if(s.top()!=’(’ || s.empty()==true)
I modified your code. You can refer to the comments for a better explanation.
Hope, this would help.
Give alike, if you are satisfied.