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]
4 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]4 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
Uses of keyboard as a input device
KATRIN_1 [288]

helps us for typing letters, numbers and symbols etc.

3 0
3 years ago
Read 2 more answers
Which part holds the "brains" of the computer?
Rus_ich [418]
The (CPU) holds the “brains” of the computer
7 0
3 years ago
Which of the following best describes the safety of blogging
Pavlova-9 [17]
We need the options
3 0
3 years ago
ANSWER QUICKLIY
lidiya [134]

Answer:

Explanation:

Allow you to view the document in different ways. Sorry if I am wrong. I am dumb

God bless, stay safe, and good luck! :)

4 0
3 years ago
Read 2 more answers
Write a program that has a conversation with the user. The program must ask for both strings and numbers as input. The program m
konstantin123 [22]

Answer:

yo im sorry eat my cookie

Explanation

doorkoeeworkwoeroewkrwerewrwe

5 0
3 years ago
Other questions:
  • What do you call a named collection of data stored on a disk?
    15·1 answer
  • What are the synonyms for each word? :
    5·1 answer
  • How do you find specific words on a web page ...?? I know theres a shortcut for highlighting a specific word on a webpage on fir
    15·1 answer
  • Which model allows you to make subsystems in parallel?
    12·1 answer
  • Beyond adding equations, what else does the Insert Equation feature allow users to do?
    7·1 answer
  • Help me pls!!! last question
    9·1 answer
  • What are the physical aspect of a presentation​
    7·1 answer
  • A large company has a LAN. The manager of the company wants to change it to a WAN
    10·1 answer
  • As we move up a energy pyrimad the amount of a energy avaliable to each level of consumers
    10·1 answer
  • What is the sequence of instructions performed to execute one program instruction
    13·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!