Advertisement
Rakibul_Ahasan

Stack_Parentheses_balanced

Aug 22nd, 2019
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.81 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<string.h>
  3.  
  4. int is_balanced(char input[])
  5. {
  6.     char stack[100],last_char;
  7.     int top,i,len;
  8.  
  9.     top=0;
  10.     len=strlen(input);
  11.  
  12.     for(i=0;i<len;i++){
  13.         if(input[i]=='('){
  14.             stack[top]='(';
  15.             top++;
  16.            }
  17.            else if(input[i]==')'){
  18.             if(top==0){
  19.                 return 0;
  20.             }
  21.             top--;
  22.             last_char=stack[top];
  23.             if(last_char!='('){
  24.                 return 0;
  25.                }
  26.            }
  27.     }
  28.     if(top==0){
  29.         return 1;
  30.     }else {
  31.     return 0;}
  32. }
  33.  
  34. int main()
  35. {
  36.     char input[100];
  37.     scanf("%s", input);
  38.     if(is_balanced(input)){
  39.         printf("%s is balanced\n",input);
  40.     }else {
  41.         printf("%s is not balanced\n",input);
  42.     }
  43.     return 0;
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement