forked from sachin-nono/codingpractice
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRecursionIsBalanced.cpp
More file actions
67 lines (47 loc) · 1.15 KB
/
RecursionIsBalanced.cpp
File metadata and controls
67 lines (47 loc) · 1.15 KB
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/*
Recursion - Is Balenced
Take as input a string.
The string is a mathematical expression e.g. "[a+{b+(c+d)+e}+f]".
Write a recursive function which tests if the brackets in expression are balanced or not and
returns a boolean value.
*/
#include<iostream>
using namespace std;
int main()
{
char str[40];
bool isBalanced(char []), x;
cout<<"Enter a string :\n";
gets(str);
x = isBalanced(str);
cout<<endl<<x<<endl; //boolean value get printed i.e. either 1 or 0
return 0;
}
int i=0, a=0, b=0, c=0, d=0, e=0, f=0;
bool isBalanced(char str[])
{
if(str[i]!='\0')
{
if(str[i]=='[')
++a;
else if(str[i]==']')
++b;
else if(str[i]=='{')
++c;
else if(str[i]=='}')
++d;
else if(str[i]=='(')
++e;
else if(str[i]==')')
++f;
++i;
isBalanced(str);
}
if(a!=b)
return false;
if(c!=d)
return false;
if(e!=f)
return false;
return true;
}