Q:

How to check if total user input has been encased in a bracket

I have created a Boolean algebra simplifier. However, to do this, I created a subroutine that simplified brackets first and to save hassle, I then decided to just add brackets to the beginning and end of a user's inputted expression and pass it through the subroutine, to reduce the amount of code needed.
My program should only add brackets to the beginning and end of a user's input if the whole expression has already not been encased in brackets. For example,
the program would leave the expression: (A+C•B) as it is encased fully. The code will encase the following expression: A+C•B to then be this: (A+C•B). 
The way I check if an expression is encased is to check if the first character in the string is ( and if the last character in the string is ). This is pretty simple. 
However, in cases such as this: (A+C)•(B+D), as you can see the beginning and end characters are the open and close brackets respectively but the expression is not fully encased in brackets. It should be ((A+C)•(B+D)). 
I have tried to create a condition which would allow me to realise this type of exception and still encase the brackets but I am unsure how. I have tried ignoring the first bracket and adding one to a variable for every other open bracket in the expression and subtracting one from the variable for every closed bracket in the variable. 
This gives -1 if the expression is fully encased but also does for situations like the one given above. I want to ask if anyone knows how I could go about checking if an expression is fully encased in brackets that will work for every possibility. I cannot think of one. 
I know that there is no code provided but think that is a suitable question for such a site because of the issue. 

A:

You provided limited info about your exact implementation, but I'll assume your expressions are in a string.
If you think about the issue, it boils down to checking if the first bracket "gets closed" before the last character in the expression.
public static bool IsFullyEncased(string expression)
    {
        if (expression.Length > 1 && expression[0] == '(' && expression[expression.Length-1] == ')')
        {
            // Candidate for a fully enclosed expression: ( .* )
            int openBracketCount = 1;
            for (int i = 1; i < expression.Length - 1; ++i)
            {
                switch (expression[i])
                {
                    case '(':
                        openBracketCount += 1;
                        break;
                    case ')':
                        openBracketCount -= 1;
                        break;

                }
                // Additionally checks for valid expression (in terms of brackets)
                if (openBracketCount <= 0) return false;

            }
            // The last element is a bracket so we must have only 1 open
            if(openBracketCount == 1) return true; 

        }
        return false;
    }

See it in action with some tests (with valid expressions): https://ideone.com/44KV5F

