Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //TRY THIS OUT
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- int top = -1;
- char s[100];
- void balanced_parentheses(){
- int i,l;
- char exp[100],x,y;
- printf("Enter the exp: \n");
- scanf("%s",exp);
- l = strlen(exp);
- for(i = 0;i<l;i++){
- if(exp[i]=='(' || exp[i]=='{' || exp[i]=='['){
- x = exp[i];
- push(exp[i]);
- }
- else if(exp[i]==')' || exp[i]=='}' || exp[i]==']'){
- y = exp[i];
- pop(y);
- }
- }
- }
- void push(char x){
- top++;
- s[top] = x;
- }
- void pop(char y){
- if(y==')'){
- if(s[top]=='('){
- top=top-1;
- }
- }
- else if(y=='}'){
- if(s[top]=='{'){
- top = top-1;
- }
- }
- else if(y==']'){
- if(s[top]=='['){
- top = top-1;
- }
- }
- else{
- return "\0";
- }
- }
- void Print(){
- int i;
- if(top==-1){
- printf("\nThey are balanced parentheses");
- }
- else{
- printf("\nFALSE! not balanced ones..because\n");
- for(i=0;i<=top;i++){
- if(s[top]=='('){
- printf("You should end with: ')' ");
- }
- else if(s[top]=='{'){
- printf("You should end with: '}' ");
- }
- else if(s[top]=='['){
- printf("You should end with: ']'");
- }
- else{
- printf("INVALID expression here!");
- }
- return;
- }
- }
- }
- int main(){
- balanced_parentheses();
- Print();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement