Home > OS >  How to make a function that checks the validity of brackets in a string?
How to make a function that checks the validity of brackets in a string?

Time:11-04

I have coded a function that check if brackets in a certain string are valid and returns true if it is and false if it isn't.

For example:

str1: { [ a b ] - ] ( c - d } ] = false.

str2: { [ a b ] - ( c - d ) } = true.

When I run the program it doesn't give any output, just a blank output.

What do I need to change?

public static Boolean BracketCheck(string str)
{
    Stack<char> stk = new Stack<char>();
    Stack<char> aid = new Stack<char>();
    Stack<char> temp = new Stack<char>();
    while (str != "")
    {
        char ch = str[0];
        if(ch == '(' || ch == '{' || ch == '[' || ch == ')' || ch == '}' || ch == ']')
        {
            stk.Push(ch);
        }
        if(str.Length != 1)
            str = str.Substring(1, str.Length - 1);
    }
    stk = Opposite(stk);
    char first = stk.Pop();
    char last;
    while (!stk.IsEmpty() && !aid.IsEmpty())
    {
        while (!stk.IsEmpty())
        {
            aid.Push(stk.Top());
            last = stk.Pop();
            if (stk.IsEmpty())
                if (int.Parse(first   "")   1 != int.Parse(last   "") || int.Parse(first   "")   2 != int.Parse(last   ""))
                {
                    return false;
                }
        }
        first = aid.Pop();
        while (!aid.IsEmpty())
        {
            aid.Push(aid.Top());
            last = aid.Pop();
            if (aid.IsEmpty())
                if (int.Parse(first   "")   1 != int.Parse(last   "") || int.Parse(first    "")   2 != int.Parse(last   ""))
            {
                return false;
            }
        }
        first = stk.Pop();

    }
    return true;
}
public static Stack<char> Opposite(Stack<char> stk)
{
    Stack<char> temp = new Stack<char>();
    while (stk.IsEmpty())
    {
        temp.Push(stk.Pop());
    }
    return temp;
}

CodePudding user response:

You are on the right way (Stack) but it should be just one, not three. To check brackets validity only:

public static Boolean BracketCheck(string str) {
  if (string.IsNullOrEmpty(str))
    return true;

  Stack<char> expected = new Stack<char>();

  foreach (char c in str) {
    if (c == '(')
      expected.Push(')');
    else if (c == '[')
      expected.Push(']');
    else if (c == '{')
      expected.Push('}');
    else if (c == ')' || c == ']' || c == '}') {
      if (expected.Count == 0 || expected.Pop() != c)
        return false;
    }
  }

  return expected.Count == 0;
}

If you want to validate the string as a formula, e.g. (3 ) 5 has valid brackets, but is invalid formula, have a look at shunting yard algorithm

CodePudding user response:

You created aid and did nothing with it before the line while (!stk.IsEmpty() && !aid.IsEmpty()) so aid is empty and nothing in that loop ever runs.

There's also a bunch of things that might be better asked on the code review site; for example you don't need to remove characters from a string to iterate over the characters in it or convert chars to strings to integers to compare them. Essentially what you want to do is create a stack, iterate over the string, any opening bracket push to the stack, any closing bracket pop the stack and check the opening bracket matches, and if at the end of the string the stack is empty then it is valid. You don't need all the reversing and creating second stack stuff.

CodePudding user response:

this works for me do you see any issuse or points to improve on?

public static Boolean BracketCheck(string str)
    {
        Stack<char> stk = new Stack<char>();
        foreach(char c in str)
        {
            if (c == '(' || c == '[' || c == '{')
            {
                stk.Push(c);
            }
            else if (c == ')' || c == ']' || c == '}')
            {
                if (stk.Top() == (int)c - 1 || stk.Top() == (int)c - 2)
                {
                    stk.Pop();
                }
            }
            
        }
        return stk.IsEmpty();
    }
  • Related