Balanced Parantheses using Stack C Program

 

C Program To Check Balanced Parantheses using Stack Data Structure

Learn How To Check Balanced Parantheses using Stack Data Structure in C Programming Language. This C Program checks if Parantheses of an Expression are Balanced or Not using Stack Data Structure. If the parantheses do not match or if the Number is not even, then the expression will have unbalanced parantheses. This C Program checks Nesting of Parantheses in an Expression as well.

The parantheses used in an algebraic expression are: { [ ( and its matching Right parantheses. The Number of Paratheses should, therefore be even or in other words, the opening parantheses should be equivalent to the closing parantheses. For every opening bracket, there should be a similar closing bracket.

 
   

Algorithm To Check if Parantheses are Balanced or Not

  • Declare A Stack
  • Input Algebraic Expression from the User
  • Traverse the Expression
    • Push the Current Character to Stack if it is an Opening Parantheses such as (, [ or {.
    • Pop the Current Character from Stack if the Expression has a Closing Bracket such as ), ] or }.
      • If the Popped Character is matching the starting parantheses, then the Expression is Balanced else it includes Unbalanced Parantheses.
  • After Traversal is completed, if there is any remaining Left Bracket in the Stack, then the Parantheses are not Balanced.

Must Read: C Program To Reverse a Stack String

   

C Program To Check Balanced Parantheses using Stack in an Algebraic Expression

Output

Check Balanced Parantheses using Stack Data Structure in C Programming

If you have any compilation errors or doubts in this Data Structure Program to Find Balanced Parantheses in C Programming, let us know about in the Comment Section below. Find more about Stack and Queues on cs.cmu.edu.

6 thoughts on “Balanced Parantheses using Stack C Program

  1. Rahul Vaidya

    Thanks for the algorithm! This is too good! It has helped me to understand the program code with much ease.

    Reply
  2. Kush Vidhate

    You have simplified the code for balanced parantheses. I finally could understand the logic now.

    Reply

Let's Discuss