C Program to Check Expression is Correctly Parenthesized

This is a C Program to check if expression is correctly parenthesized.

Problem Description

This program takes a expression as input and checks if the expression is correctly parenthesized.

Problem Solution

1. Take a expression as input and store it in the array.
2. Check for the “(” and “)” in the expression.
3. If “(” encounters, then push it to the separate array. If “)” encounters, then pop the element of the array.
4. If the number of “(” and “)” are equal, then the expression is correctly parenthesized. Otherwise it is not.

Program/Source Code

Here is source code of the C Program to check if expression is correctly parenthesized. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /*
  2.  * C Program to Check if Expression is correctly Parenthesized  
  3.  */
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7.  
  8. int top = -1;
  9. char stack[100];
  10.  
  11. // function prototypes
  12. void push(char);
  13. void pop();
  14. void find_top();
  15.  
  16. void main()
  17. {
  18. 	int i;
  19. 	char a[100];
  20. 	printf("enter expression\n");
  21. 	scanf("%s", &a);
  22. 	for (i = 0; a[i] != '\0';i++)
  23. 	{
  24. 		if (a[i] == '(')
  25. 		{
  26. 			push(a[i]);
  27. 		}
  28. 		else if (a[i] == ')')
  29. 		{
  30. 			pop();
  31. 		}
  32. 	}
  33. 	find_top();
  34. }
  35.  
  36. // to push elements in stack
  37. void push(char a)
  38. {
  39. 	stack[top] = a;
  40. 	top++;
  41. }
  42.  
  43. // to pop elements from stack
  44. void pop()
  45. {
  46. 	if (top == -1)
  47. 	{
  48. 		printf("expression is invalid\n");
  49. 		exit(0);
  50. 	}	
  51. 	else
  52. 	{		
  53. 		top--;
  54. 	}
  55. }
  56.  
  57. // to find top element of stack
  58. void find_top()
  59. {
  60. 	if (top == -1)
  61. 		printf("\nexpression is valid\n");
  62. 	else
  63. 		printf("\nexpression is invalid\n");
  64. }
Program Explanation

1. Take a expression as input and store it in the array a[]. Initialize the variable top to -1.
2. Using for loop check for the “(” and “)” in the expression.
3. If “(” encounters, then push the character to the separate array stack[] and increment the variable top by 1. If “)” encounters, then pop the top most element of the array stack[] and decrement the variable top by 1. Use variable top to denote the top element of the array stack[].
4. If the variable top is equal to -1, then the expression is correctly parenthesized. Otherwise it is not.

advertisement
advertisement
Runtime Test Cases
enter expression
(a+b)
expression is valid
 
 
enter expression
(a+b))
expression is invalid
 
 
enter expression
((a+b)
expression is invalid
 
 
enter expression
((a+b)*(c+d))
expression is valid

Sanfoundry Global Education & Learning Series – 1000 C Programs.

Here’s the list of Best Books in C Programming, Data-Structures and Algorithms

Note: Join free Sanfoundry classes at Telegram or Youtube
If you wish to look at programming examples on all topics, go to C Programming Examples.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.