I am reading the Algorithm Design Manual Second Edition and this is from an exercise question. Quoting the question
A common problem for comp
int i;
int len;
char popped;
stack st;
string a = "({<<";
len = a.length();
for(i=0;i' || a[i] == ')' || a[i] == ']' || a[i] == '}')
{
if(st.empty())
{
cout << "stack is empty, when popped , not balanced" << endl;
return 0;
}
else
{
popped = st.top();
st.pop();
if (!((a[i] == '>' && popped == '<') || (a[i] == ')' && popped == '(') || (a[i] == '}' && popped == '{') || (a[i] == '>' && popped == '<'))) //ok
{
cout << "not balanced on character" + std::string(1,a[i]) << endl;
return 0;
}
}
}
}
if(st.empty())
{
cout << "balanced" << endl;
}
else
{
cout << "not balanced stack not empty" << endl;
}