Jun 27, 2009

parenthesis matching onlu “(“ and ”)”

parenthesis matching on “(“ and ”)”
bool foo(char* a, int c, int idx)//initially, idx=0,c=0
{
if(strlen(a)==idx&&c==0) return true;
if(strlen(a)==idx&&c!=0) return false;
if(a[idx]==’(’) c++;
else if(a[idx]==’)’) c--;
if(c<0) return false;
return foo(a,c,idx+1);
}

bool foo(char* arr)
{
int p=0;
int c=0;
while(!arr[p])
{
if(c<0) return false;
if(arr[p]=='(') c++;
if(arr[p]==')') c--;
p++;
}
if(c==0) return true;
else trturn false;
}

No comments:

Post a Comment