C Program To Check Matching Parantheses without Stack
Learn How To Check Matching Parantheses without Stack in C Programming Language. This Matching Parantheses C Program takes a String as an Input from the User. The String contains the Algebraic Expression.
The Matching Parantheses Algorithm is very simple. It counts the number of Opening Parantheses and Closing Parantheses. If both are equal, then the Parantheses are Matching in the Expression, else the Algebraic Expression is an Unbalanced Expression. This Parantheses Matching Problem is also famously known as Onion Peeling Problem. However this algorithm only focuses on the count of opening and closing parantheses. It does not check the exact parantheses. This can be done using Stack as the Data Structure instead of a String Array.
Must Read: C Program To Check Balanced Parantheses in an Expression
This Code traverses the Expression String and counts the number of Opening and Closing Brackets. The Parantheses used in an Algebraic Expression are:
Opening Parantheses / Brackets: { [ (
Closing Parantheses / Brackets: } ] )
Must Read: C Program To Reverse a String using Stack Data Structure
C Program For Checking Matching Parantheses in an Expression without Stack
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | #include<stdio.h> int main() { char str[100]; int count, limit, open_count, close_count; printf("\nEnter the Limit of Expression Characters:\t"); scanf("%d", &limit); printf("\nEnter an Algebraic Expression:\t"); scanf("%s", str); for(count = 0; count < limit; count++) { if(str[count] == '{' || str[count] == '[' || str[count] == '(') { open_count++; } else if(str[count] == '}' || str[count] == ']' || str[count] == ')') { close_count++; } } if(open_count == close_count) { printf("\nThe Algebraic Expression has matching Parantheses\n"); } else { printf("\nThe Algebraic Expression does not have matching Parantheses\n"); } return 0; } |
Must Read: C Program To Create Own Header Files
Output

If you have any compilation errors or doubts in this C Program To Find if Parantheses are matching or Not without using Stack Data Structure, let us know about in the Comment Section below. Find more about Stack Data Structure on cs.cmu.edu.
This is the best explanation and code I found for checking if paranthese are matching or not. This is such an easy code.
[(])
this cannot be a balanced expresson.
can i get code for this.