C Program to Check if 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.

advertisement
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
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 Reference Books in C Programming, Data-Structures and Algorithms

advertisement
If you wish to look at programming examples on all topics, go to C Programming Examples.

advertisement
advertisement
advertisement
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn