Hi everyone,
someone send me this function to check the brackets whether they are matching or not but I could not get it work. Can anyone help me to get this work please... I want to ask the user to enter some text and braces
e.g. Enter some text: (sdfds()fd) hit enter result braces matching
another e.g Enter some text: (Dsfg([ )e)ef user hits enter and result is braces not matching. Help please...
int balanced(const char * data, const char * left, const char * right)
{
int i, good = 1, top = 0, count = strlen(left);
if(strlen(right) != count) { // sanity check
return 0;
}
char * stack = malloc(strlen(data));
if(stack == NULL) {
perror("malloc");
return 0;
}
for(const char * p = data; *p && good; p++) {
for(i = 0; i < count; i++) {
if(*p == left[i]) {
stack[top++] = left[i];
} else if(*p == right[i]) {
if(top == 0 || (stack[--top] != left[i])) {
good = 0;
break;
}
}
}
}
free(stack);
return good && top == 0;
}