Advertisement
Shailrshah

Parentheses Check

Aug 31st, 2013
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.06 KB | None | 0 0
  1. #include <stdio.h>
  2. char stack [100];
  3. int top;
  4. void pop(){
  5.     top--;
  6. }
  7. void push(char n){
  8.     stack[top++] = n;
  9. }
  10. char peek(){
  11.     return stack[top-1];
  12. }
  13. int main(){
  14.     char str[100];
  15.     int neg = 0;
  16.     printf("\nEnter an expression(! to exit): ");
  17.     gets(string);
  18.     for(int i = 0; string[i] != '\0'; i++){
  19.         switch(string[i])
  20.         {
  21.             case '(':   push('('); break;
  22.             case '[':   push('['); break;
  23.             case '{':   push('{'); break;
  24.             case ')':   if(peek() == '(') pop();
  25.                         else neg = 1;
  26.                         break;
  27.             case ']':   if(peek() == '[') pop();
  28.                         else neg = 1;
  29.                         break;
  30.             case '}':   if(peek() == '{') pop();
  31.                         else neg = 1;
  32.             default:    continue;
  33.         }          
  34.     }
  35.     if(top) printf("\nParenthesis Not Valid! Closing Missing.");
  36.     if(neg) printf("\nParenthesis Not Valid! Opening Missing.");
  37.     if(!top && !neg) printf("\nParenthesis Valid!");
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement