Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save AakashCode12/ab6741f1442fb31d285244dd85c98197 to your computer and use it in GitHub Desktop.
Save AakashCode12/ab6741f1442fb31d285244dd85c98197 to your computer and use it in GitHub Desktop.
/*
Practical no 2 --parenthisis check
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//variable declarations
int top = -1;
char stack[100];
// function definitions or prototyping
void push(char);
void pop();
void find_top();
// to push elements in stack
void push(char a){
stack[top] = a;
top++;
}
// to pop elements from stack
void pop(){
if (top == -1){
printf("This Expression is not Valid\n");
exit(0);
}
else{
top--;
}
}
// to find top element of stack
void find_top(){
if (top == -1)
printf("\nThis Expression is Valid\n");
else
printf("\nThis Expression is not Valid\n");
}
//main function
int main()
{
int i;
char a[100];
printf("Please Enter the Expression\n");
scanf("%s", &a);
for (i = 0; a[i] != '\0';i++){
if (a[i] == '('||a[i] == '['||a[i] == '{'){
push(a[i]);
}
else if (a[i] == ')'|a[i] == ']'|a[i] == '}'){
pop();
}
}
find_top();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment