Professional Documents
Culture Documents
Data Structures and Algorithms LAB CAT
Data Structures and Algorithms LAB CAT
LAB CAT-1
DATE: 05.04.2022
Register Number: 21MIS0332
Name: K. KAUSHIK
Aim: C program to check whether given expression
contains any duplicate parenthesis or not and Display
the duplicate parenthesis.
Algorithm:
1.CREATE A STACK AND SET TOP TO -1.
2.TAKE EXPRESSION AS INPUT FRON USER
3.CHECK THE CHARACTER OF EXPRESSION ONE
BY ONE BY FOR LOOP AND PUSH IF IT IS A
BRACKET.
4.CHECK FOR THE CORRESPONDING CLOSING
BRACKET.IF IT IS PRESENT,THEN PROCEED.
5. SET COUNT TO 0 AT BEGINNING.IF MORE THAN
ONE BRACKET IS PRESENT,INCREASE COUNT TO
1.
6.IF COUNT=1,THEN PRINT “ IT HAS A DUPLICATE
BRACKET”.
7.PRINT THE DUPLICATE BRACKET.
Program:
#include <stdio.h> /*KAUSHIK 21MIS0332*/
#include <string.h>
char stack[20];
int top = -1;
int main(){
int isOpenB(char);
int isCloseB(char);
char oppositeOf(char);
void push (char);
char expression[20];
printf("Enter the expression : ");
scanf("%s",expression);
else if (isCloseB(expression[i])){
if (stack[top] == oppositeOf(expression[i])){
top--;
}
else {
printf("Mismatching '%c' and '%c'\n", stack[top], expression[i]);
break;
}
}
return 0;
}