Thread: Brace matching

  1. #1
    Registered User
    Join Date
    Jul 2004
    Posts
    7

    Brace matching

    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;
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by coskun
    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...
    Someone huh? You mean THIS SOMEONE?!

    1) Stop making new threads for the same topic.
    2) USE CODE TAGS.
    3) While you're at it, go read the Announcements.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Jul 2004
    Posts
    7
    Woawww seems you ........ed of mate. sorry if I made a mistake by using this forum. I am quite novice to this. I just became member yesterday. nobody were born knew everything, I am learning C and how to use this forum, thanks for helping...

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    What irked me was the fact that you said "someone sent me" ... no. Noone sent you anything. It was posted on this public forum, not sent to you. It came across as you saying you had magicly found a function out of the blue, with no credit to the author.

    Well, that and the fact that you didn't read or follow the forum rules...

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Jul 2004
    Posts
    7
    Ok Quzah next time when someone posts something on my requests and if I need to ask question related to that bit of code I will mention the persons name as well so the author can make some credit. Hope this makes you happy... I am not a kind of person who likes advertising himself that's why I couldn't think some people does...
    Coskun

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You missed the point. And now we're way off topic. You could always just delete this thread.

    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Actually, he can't, but I can close it.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 06-01-2009, 07:54 PM
  2. Matching numbers
    By kirksson in forum C Programming
    Replies: 7
    Last Post: 07-23-2008, 01:51 PM
  3. Matching the brackets
    By coskun in forum C Programming
    Replies: 27
    Last Post: 07-30-2004, 11:41 AM
  4. Replies: 5
    Last Post: 04-16-2004, 01:29 AM
  5. Going out of scope
    By nickname_changed in forum C++ Programming
    Replies: 9
    Last Post: 10-12-2003, 06:27 PM