-
Notifications
You must be signed in to change notification settings - Fork 0
/
Bracketcheck.cpp
46 lines (44 loc) · 893 Bytes
/
Bracketcheck.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <iostream>
#include<stdio.h>
#include<stdlib.h>
#include<stack>
#include<string.h>
char rev(char);
using namespace std;
int main()
{
char exp[20];
cout << "Enter an expressionn::";
gets(exp);
stack<char>s;
for(int i=0;i<strlen(exp);i++)
{
if(exp[i]=='('||exp[i]=='['||exp[i]=='{')
s.push(exp[i]);
else if(exp[i]==')'||exp[i]==']'||exp[i]=='}')
{
if(s.empty())
{
cout<<"NOT a valid expression";
exit(0);
}
if(rev(exp[i])==s.top())
{
s.pop();
}
}
}
if(s.empty())
cout<<"valid Expression";
else cout<<"not a valid expression";
return 0;
}
char rev(char ch)
{
if(ch==')')
return '(';
if(ch=='}')
return '{';
if(ch==']')
return '[';
}