Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- char stack [100];
- int top;
- void pop(){
- top--;
- }
- void push(char n){
- stack[top++] = n;
- }
- char peek(){
- return stack[top-1];
- }
- int main(){
- char str[100];
- int neg = 0;
- printf("\nEnter an expression(! to exit): ");
- gets(string);
- for(int i = 0; string[i] != '\0'; i++){
- switch(string[i])
- {
- case '(': push('('); break;
- case '[': push('['); break;
- case '{': push('{'); break;
- case ')': if(peek() == '(') pop();
- else neg = 1;
- break;
- case ']': if(peek() == '[') pop();
- else neg = 1;
- break;
- case '}': if(peek() == '{') pop();
- else neg = 1;
- default: continue;
- }
- }
- if(top) printf("\nParenthesis Not Valid! Closing Missing.");
- if(neg) printf("\nParenthesis Not Valid! Opening Missing.");
- if(!top && !neg) printf("\nParenthesis Valid!");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement