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
wolverine [178]
3 years ago
13

Programming challenge description: Given a string comprising just of the characters (,),{,},[,] determine if it is well-formed o

r not. Input: Your program should read lines from standard input. Each line contains a string comprising of the characters mentioned above. There is no whitespace between characters. Output: Print out True if the string is well-formed, False otherwise.
Computers and Technology
1 answer:
nadezda [96]3 years ago
5 0

Answer:

Employing Java language,

Explanation:

//for using stack data structure

import java.util.Stack;

//class definition

public class BracketMatch {

   //function to find the string is well formed or not

   public static boolean isWellFormed(String str) {

       //variable to store the character value

       char ch;

       //check if the first character itself is the end of the brackets

       if (str.charAt(0) == ')'||str.charAt(0) =='}'||str.charAt(0) ==']')

           return false;

       //creating a stack of character type

       Stack<Character> stack = new Stack<Character>();

       //iterating through the characters in string

       for(int i=0; i < str.length(); i++) {

           //storing the current character from the string in the variable

           ch = str.charAt(i);

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

               //if the character is starting of the brackets push it into stack

               stack.push(ch);

           else if(ch == ')')

               //if it is closing bracket and the stack is empty then the string is not well formatted

               if(stack.empty())

                   return false;

               else if(stack.peek() == '(')

                   stack.pop();

               else

                   return false;

           else if(ch == '}')

               if(stack.empty())

                   return false;

               else if(stack.peek() == '{')

                   stack.pop();

               else

                   return false;

       }

       //returning the stack content, if the string will be well formed the stack would be empty

       //after all the operations

       return stack.empty();

   }

   //main method

   public static void main(String[] args) {

       //calling function to find if the string is welll formatted or not

       System.out.println(isWellFormed("{}"));

       //calling function to find if the string is welll formatted or not

       System.out.println(isWellFormed("([)]"));

   }

}

You might be interested in
BRAINLIEST Which function will add a name to a list of baseball players in Python?
Tju [1.3M]

Answer: Append()

Explanation:

8 0
3 years ago
Read 2 more answers
A line of text that has an equal number of characters on either side of the horizontal center of the page is called____.
VLD [36.1K]
A) force justified.

If you open up Word (or probably any word processor) and hover over the alignment options in the ribbon (or format the paragraph), it tells you what each one is, and the option that ensures the characters are spread evenly between the margins is labeled "justified."
6 0
3 years ago
What type of device is the printer?
ICE Princess25 [194]

B,the printer only receives signals from the computer and prints them.

4 0
3 years ago
Read 2 more answers
Can I have help on this
never [62]

the answer is the seconf one


6 0
3 years ago
Read 2 more answers
19. Fair use applies to which of the following situations?
MatroZZZ [7]
I think it’s B if not it’s D both are similar
4 0
3 years ago
Other questions:
  • The sequence of folders to a file or folder is known as its _______.
    6·1 answer
  • The process of engineering design typically starts with what ?
    12·1 answer
  • What is the purpose of an arraignment?
    9·2 answers
  • What is the ability for a system to respond to unexpected failures or system crashes as the backup system immediately and automa
    8·1 answer
  • Danica is creating a flyer for her cookies that she will sell during her school fair. She wants to add a registered
    7·1 answer
  • Unleashes the ability of each person on their team to improve performance, solve problems, and
    6·2 answers
  • In this exercise, you will get some practice with the __add__ method by implementing it for a class called ContactBook. This cla
    12·1 answer
  • What is the meaning of s used in 0 and 1 in computer .​
    8·1 answer
  • Choose the missing term.
    14·1 answer
  • Write a program that takes in an integer in the range 10 to 100 as input. Your program should countdown from that number to 0, p
    13·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!