SCHOOL OF CODE BUILDERS
Learn To CODE. Become A DEVELOPER.
Pages
HOME
DATA STRUCTURES
STRINGS
ARRAYS
MATRIX
BINARY TREES
LINKED LIST
STACK
QUEUE
SORTING
SEARCHING
C
PYTHON
PSEUDOCODE
CONTEST PROBLEMS
ALGORITHMS
PATTERNS
PHP
C PUZZLES
C INTERVIEW QUESTIONS
JAVA
C++
HASHING
RECURSION
BASIC C PROGRAMS
TCS-CODEVITA
FACEBOOK
CONTACT US
C program to balance parentheses
C program to balance parentheses
Output:
Enter the expression:
( ( ( a + b ) ( ( c + d ) ) )
The resultant expression is: ( ( a + b ) ( ( c + d ) ) )
Implementation:
#include
#include
#include
#define MAX 100 int main(void) { char *s; int l,r,i,n; int hash[MAX]; s=(char*)malloc(sizeof(char)*MAX); printf("Enter the expression:\n"); scanf("%s",s); memset(hash,0,sizeof(hash)); l=0; n=strlen(s); while(*(s+l)!='(') { //hash[l]=1; l++; } r=l+1; while(*(s+r)) { if(*(s+r)==')' || hash[r]==0) { l=r; while(*(s+l)!='(' && hash[l]!=1) { l--; } hash[l]=1; hash[r]=1; /* while(*(s+l)!='(') l++; while(*(s+r)!=')') r++; */ } r++; } /*for(i=0;i
PREVIOUS
NEXT
HOME