Thread: BracketProblems

  1. #1
    Registered User
    Join Date
    Aug 2004
    Posts
    8

    BracketProblems

    How could I get this code working correctly. At the moment I am only checking the amount of braces but I want to check their orders as well. In this case they are matching but normally it should give me an error. What's is the correct way to fix this or can some1 show me another way to do it. Thank you.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main (void)
    {
    int square=0, curly=0, round=0;
    int i;
    char userstr[21] = "(asdf[sd)ds]nk";
    
    
    for(i=0;i<strlen(userstr);i++)
    {
        if(userstr[i]=='(')
            round++;
        else if(userstr[i]==')')
            round--;
        
         else if(userstr[i]=='{')
            curly++;
        else if(userstr[i]=='}')
            curly--;
    
        else if(userstr[i]=='[')
            square++;
        else if(userstr[i]==']')
            square--;
    
    }
    
    if(round!=0 ||square !=0 || curly!=0){
    
        printf("They are not matching try again\n");
     }
     else 
        printf("They are matching\n");
    return 0;
    }

  2. #2
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    If a counter (round, curly, or square) is zero, and the program sees a right bracket of that kind --- ),}, or ] respectively --- that's an error.

    Additional point: Is it illegal to have something like:

    something{something else[abc}]

    The brackets are matched, but one phrase is only partially nested in the other. Your program does not take this into account. Maybe that's not something you have to check, but I just thought I would mention it.

    Regards,

    Dave
    Last edited by Dave Evans; 08-26-2004 at 07:35 AM.

  3. #3
    Registered User
    Join Date
    Aug 2004
    Posts
    8
    Auctially that was what I wanted to mention. I don't want to have nested brackets too but the problem I have no idea how to do that? Help please...

  4. #4
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    I'm sorry that I answered a question that you didn't ask. Changes suggested by my post would flag the following as an error, whereas your original program would not:

    (somethingsomthing else))(this is more stuff



    Now, how do you detect improper nesting, as in your example string:

    "(asdf[sd)ds]nk"

    Well, I'm not sure about the best way for a program to detect the problem, but think about how you can see that it's wrong :

    Pretend that you are the program: Three counters for the three types of bracket. The program increments a counter when encountering a left bracket and decrements that counter when encountering a right bracket.

    When you encounter a left bracket of a particular type, note the counts of the other brackets. When you encounter a right bracket, see if either of the other counters is less than it was. If so: it's an error.

    How would you implement this in a program? Lots of ways. Are there other ways of detecting the improper nesting? Sure. If you can see a better way, go for it.

    Regards,

    Dave

  5. #5
    Registered User
    Join Date
    Aug 2004
    Posts
    8
    I found myself in the infinite loop please do not hesitate to show me your way. I am stuck there.. Help please

  6. #6
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    I don't really have a way. I just threw out something for you to consider, hoping that you (or others reading this board) would start thinking about how to approach the problem.

    The sequence that I outlined would require, for each bracket type, storage for the count values of the other bracket type. The count values could be stored in a stack (LIFO data buffer). One stack for each bracket type.

    Then, when a left "round" is encountered, push the values of "square" counter and "curly" counter onto the "round" stack. When a right "round" is encountered, pop the values of the "square" and "curly" counter, and compare with current values of "square" count and "curly" count. If either current value is different from its corresponding value off of the stack: it's an error. (Since the only way the current count for another bracket could be less is that a bracket of the other type has occurred since the last left "round".)

    (Identical code for each of the three bracket types.)

    Note that in the previous post, I said there is an error if the current value is "less than" the value off of the stack: should have said "different from".

    Does this sound like a lot of programming? Well, if you know how to implement a stack, it's not so much. There are still lots of details, like: if a stack is empty, proceed without popping (no error), etc.

    Can you think of a better way? Go for it. Meanwhile, I think this is a way to approach the problem.

    Good Luck!

    Dave

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You could always just search the forum for the topic. I know for a fact this has been discussed on at least one occasion, because I posted in a thread on it.

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

Popular pages Recent additions subscribe to a feed