1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
Rom4ik [11]
3 years ago
15

Write a function int checkBalance(char exp[]); that can take an expression as input and check whether the parenthesis used in th

at expression are valid or invalid. It returns 1 if it is valid, and returns 0 if it is not valid.For example, the following expressions are valid: [ A * {B (C D)}] and [{()()}]The following expressions are invalid: [ A * {B (C D})], [ { ( ] ) ( ) }
Computers and Technology
1 answer:
mylen [45]3 years ago
3 0

Answer:

Explanation:

The following code is written in Java. It uses for loops to loop through the array of characters, and uses IF statements to check if the symbols in the expression are balanced. If so it returns true, otherwise it breaks the function and returns false. Two test cases have been provided in the main method and the output can be seen in the attached image below.

import java.util.ArrayList;

class Brainly {

   public static void main(String[] args) {

       char[] exp = { '[', 'A', '*', '{', 'B', '(', 'C', 'D', ')', '}', ']'};

       char[] exp2 = {'[', 'A', '*', '(', 'C', '}', ']'};

       System.out.println(checkBalance(exp));

       System.out.println(checkBalance(exp2));

   }

   public static boolean checkBalance(char exp[]) {

       ArrayList<Character> symbols = new ArrayList<>();

       for (char x: exp) {

           if ((x =='[') || (x == '{') || (x == '(')) {

               symbols.add(x);

           }

           if (x ==']') {

               if (symbols.get(symbols.size()-1) == '[') {

                   symbols.remove(symbols.size()-1);

               } else {

                   return false;

               }

           }

           if (x =='}') {

               if (symbols.get(symbols.size()-1) == '{') {

                   symbols.remove(symbols.size()-1);

               } else {

                   return false;

               }

           }

           if (x ==')') {

               if (symbols.get(symbols.size()-1) == '(') {

                   symbols.remove(symbols.size()-1);

               } else {

                   return false;

               }

           }

       }

       return true;

   }

}

You might be interested in
Does the Main Content (MC) of a web page include searchboxes?
Tanzania [10]
Yes it does use search boxes
8 0
3 years ago
Read 2 more answers
For which of the following is a bulleted list more appropriate than a numbered list?
Vedmedyk [2.9K]
THE ANSWER IS A!!!!!!!!!!!!!!!!!!!
5 0
3 years ago
What is the different between the simple statement and compound statement ?​
OverLord2011 [107]

Answer:

Following is the difference between Simple and Compound statement; Simple statement (sentence) is one which has only one subject and one predicate. A Compound statement (sentence) is one which has two or more independent clauses consisting of their own subject and predicate.

Explanation:

hope it helps you

Mark as brainliest.

And follow for a follow back

4 0
2 years ago
Which payment method typically charge is the highest interest rates?
jek_recluse [69]

The highest amount is Credit. The more you wait the higher the interest, higher the interest the more you pay, and no one wants to play more. Stay safe use debit as much as you can

8 0
3 years ago
Which option correctly identifies if the researcher’s approach was the best start in the following scenario and explains why?
dem82 [27]

Answer:

The first one

Explanation:

The other ones don’t make sense

4 0
3 years ago
Read 2 more answers
Other questions:
  • Which of these is a preferred method for
    14·2 answers
  • Write a Python function merge_dict that takes two dictionaries(d1, d2) as parameters, and returns a modified dictionary(d1) cons
    5·1 answer
  • The issue with discovering a perfect solution to a problem is that ________.
    10·2 answers
  • Create a query that will list all technician names, employee numbers, and year hired in order by year hired (Newest to Oldest).
    5·1 answer
  • In the game of economics, which player has the role of providing goods and services
    10·1 answer
  • When a user utilizes another computer to communicate with a third party, with the result that the third party cannot recognize t
    8·1 answer
  • Ryan is working on a document with many secotions. For each section,he wantes to have the title bolded,underlined,an blue. Which
    9·2 answers
  • 3.3 Code Practice: Question 1
    15·1 answer
  • Create a TicTacToe class that initializes a 3x3 board of "-" values. We will use this class in future exercises to fully build o
    9·1 answer
  • An acceptable website design is one that meets
    14·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!