Question

Using recursion in java Your program will check to see if braces are balanced. For example...

Using recursion in java

Your program will check to see if braces are balanced.

For example {} is balanced { is not.

Your program will accept a string as such {}{{}{}{ and returns true or false if the braces are balanced

Ask the user to enter the brace combos.

Print balanced or unbalanced.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Program:

import java.util.Scanner;

public class CheckBalancedBraces {

//method to check to see if braces are balanced using recursion
   public static boolean isBalanced(String str)
   {
       int n = str.length();
      
       if(n==0)
           return true;
      
       if(n%2==1)
           return false;
                  
       if(str.charAt(0) == '{')
       {
           int i = str.indexOf('}');
           if(i==-1) return false;
           return isBalanced(str.substring(1,i)) && isBalanced(str.substring(i+1));
       }
       return false;
   }
  
   //main method
   public static void main (String[] args)
   {
         
       Scanner sc = new Scanner(System.in);
       System.out.print ("Enter the brace combos: ");
       s = sc.next();
boolean f = isBalanced(s);
       if(f)
           System.out.println ("Balanced");
       else
           System.out.println ("Not balanced");
}
}

Output:
Enter the brace combos: {{{}}
Not balanced

Add a comment
Know the answer?
Add Answer to:
Using recursion in java Your program will check to see if braces are balanced. For example...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Don't know how to write the code to check the braces and matching combinations(in Java) modify...

    Don't know how to write the code to check the braces and matching combinations(in Java) modify the lab below to include () as balanced pair this means your program should check balances for {{(())}} or any pattern. Once complete, write a program to test the method Class name: BalancedBraces Using recursion Your program will check to see if braces are balanced. For example {} is balanced { is not. Your program will accept a string as such {}{{}{}{ and returns...

  • Java 8 Braces You are designing a compiler for a C++ program and need to check...

    Java 8 Braces You are designing a compiler for a C++ program and need to check that braces in any given file are balanced Braces in a string are considered to be balanced if the following criteria are met: All braces must be closed. Braces come in pairs of the form 0.0andl1. The left brace opens the pair, and the right one closes it In any set of nested braces, the braces between any pair must be closed For example,...

  • A java program for this question please! Recursion: A word is considered elfish if it contains...

    A java program for this question please! Recursion: A word is considered elfish if it contains the letters: e, l, and f in it, in any order. For example, we would say that the following words are elfish: whiteleaf, tasteful, unfriendly, and waffles, because they each contain those letters. Write a recursive method called elfish(), that, given a word, tells us whether or not that word is elfish. The signature of the method should be: public static boolean elfish(String word)...

  • Write a java program that demonstrates recursion. This program can be in a java GUI frame...

    Write a java program that demonstrates recursion. This program can be in a java GUI frame or as a console application. On this one it is up to you. (It would probably work better as a console program for this particular program.) The input for this program is a number of boxes. Ask the user for this with a textbox or a prompt on the command line. Demonstrate recursion with a function called LoadTruck() that takes a number of boxes...

  • x= Suppose you are building a program for teaching kids' math. Write a java program the...

    x= Suppose you are building a program for teaching kids' math. Write a java program the does the following: 1. Ask the user if he/she wants to sign-up a. If yes continue to step 2 b. If no Display the message "Thank you, Have a nice Day 2. Ask the user to enter a username. 3. Ask the user to enter a password with at least 8 characters long 4. Write a method with the following header: public static Boolean...

  • Problem Implement (in C) an algorithm that uses a stack to check if a parentheses sequence is balanced. Note that a par...

    Problem Implement (in C) an algorithm that uses a stack to check if a parentheses sequence is balanced. Note that a parentheses sequence is balanced if it is of the form (S) or of the form (SS), where S is any balanced parentheses sequence. See the courseware for more information on balanced parentheses sequences. Implement a stack using an array Test your program on three different kinds of inputs: 1. String is unbalanced in the sense that there are more...

  • Write a program that will check a Java file for syntax errors. The program will ask...

    Write a program that will check a Java file for syntax errors. The program will ask for an input-file name and output-file name and will then copy all the code from the Java input file to the Java output file, but with the following changes: 1. Any syntax error found in the Java input file will be corrected: a. Missing semicolon b. Missing compound statements (curly braces) c. All comments have to start with // and end with a period....

  • write a program in java that asks the user for a sentence, and then returns that...

    write a program in java that asks the user for a sentence, and then returns that string with the words backwards. For example: Enter a sentence! Mary had a little lamb. Your sentence backwards: lamb. little a had Mary Write a method called reverse() that accepts a sentence as a string, and then returns that string with the words backwards. Write a main() method that gets the string from the user and then displays the string backwards. demonstrate program by...

  • Write a Java program for a fortune teller. The program should begin by asking the user...

    Write a Java program for a fortune teller. The program should begin by asking the user to enter their name. Following that the program will display a greeting. Next, the program will ask the user to enter the month (a number 1-12) and day of the month (a number 1-13) they were born. Following that the program will display their zodiac sign. Next, the program will ask the user their favorites color (the only choices should be: red, green and...

  • 1. Write a C++ program called Password that handles encrypting a password. 2. The program must...

    1. Write a C++ program called Password that handles encrypting a password. 2. The program must perform encryption as follows: a. main method i. Ask the user for a password. ii. Sends the password to a boolean function called isValidPassword to check validity. 1. Returns true if password is at least 8 characters long 2. Returns false if it is not at least 8 characters long iii. If isValidPassword functions returns false 1. Print the following error message “The password...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT