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
What would a world where we have 2^128 (340 undecillion) Internet connected devices look like? How could that much Internet usag
cluponka [151]

A world  have 2^128 (340 undecillion) of IPv addresses that helps to connect to the internet. IPv6 addresses can make it easier for working organizational teams connecting to the Internet, IP addresses that are created by IPv6 are 128 bits.

<h3>What is internet?</h3>

Internet is the system  uses the Internet protocol suite (TCP/IP)  and interconnected computer networks that communicate between two or more individuals.

Today, almost 20 IP addresses are taken by7 each home for each electronic device. The internet usage is so wide, that even a simple task  is not possible without it. Even a simple task can be done using internet.

Learn more about internet.

brainly.com/question/13308791

#SPJ1

5 0
2 years ago
Change the file name for index.html to index.php
Damm [24]

The PHP code is given below:

<h3>PHP code</h3>

if(isset($_REQUEST['login_btn'])){

       $email = filter_var(strtolower($_REQUEST['email']),FILTER_SANITIZE_EMAIL); //strtolower changes email to all lower case

       $password = strip_tags($_REQUEST['password']);

The remaining code is in the file attached.

 

Read more about PHP here:

brainly.com/question/27750672

#SPJ1

Download txt
4 0
1 year ago
Mengapakah wanita hamil memerlukan jumlah nutrien yang lebih banyak berbanding wanita tidak hamil.
Elan Coil [88]

Answer:

Because of the baby in the womb

Explanation:

Remember, a pregnant woman carries a life (the fetus) in her womb, and the unborn child depends on its mother for food. Such a complex and fascinating stage of pregnancy requires the mother to be well-nourished or else the child may die or suffer some health problems; which may even affect the mother.

5 0
3 years ago
What does the /var directory contain?
Ghella [55]

Answer:A

Explanation:

The /var Directory. /var is a standard subdirectory of the root directory in Linux and other Unix-like operating systems that contains files to which the system writes data during the course of its operation

3 0
3 years ago
To add a glow effect to WordArt text, which of the following should be done?
g100num [7]
Number 2 is the correct answer
8 0
3 years ago
Other questions:
  • A smart refrigerator can use _____ to detect when you are running low on milk, and then send a reminder to you on a wireless net
    14·1 answer
  • You can add envelopes to existing documents. <br> a. True <br> b. False
    10·1 answer
  • Answer these two if u can❤️
    5·2 answers
  • Assuming that each of the resistors in the circuit shown in the figure above has a resistance value of 100 k ohms, what will be
    11·2 answers
  • If given the chance to own manage a business,what will be the name of the business?
    11·2 answers
  • Help me plase will give brainliest
    12·2 answers
  • Complete the below method, isPalindrome(), that takes in a String and returns a boolean. The boolean should be true if the Strin
    7·1 answer
  • I need more friends plz talk with me
    6·2 answers
  • Not every organization integrates with the Internet, but all use some or most of the technology that gave rise to it.
    15·1 answer
  • You are the administrator for the ABC Company. You are looking to install Windows Server 2016, and you need to decide which vers
    13·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!