#define IsLeft(a) (a=='(' || a=='{' || a == '[')
#define IsMatch(a,b) (a=='('&& b==')' \
|| a == '{' && b =='}' \
|| a=='[' && b == ']')
bool IsValidParentheses(const char* p, int l)
{
if (l %2 == 1 || !IsLeft(*p))return false;
char stack[l];
int index = 0;
char c;
while(*p)
{
c = *p++;
if(IsLeft(c))
stack[index++] = c;
else
{
if (IsMatch(stack[index-1],c))
--index;
else
return false;
}
}
return index <= 0;
}
#include <stdio.h>
int main(int argc, char** argv)
{
char a[]="[]{}";
char b[]="{[]()}";
char c[]="{{{}[]})";
printf("%s is %s Parentheses\n",
a, IsValidParentheses(a,4)?"valid":"invalid");
printf("%s is %s Parentheses\n",
b, IsValidParentheses(b,6)?"valid":"invalid");
printf("%s is %s Parentheses\n",
c, IsValidParentheses(c,8)?"valid":"invalid");
return 0;
}