0% found this document useful (0 votes)
87 views2 pages

Bracket Matching Assignment

The document describes an implementation of a bracket matching algorithm using a stack in Java. It defines methods to check if a bracket is left or right, if two brackets match, and performs the matching by iterating through the string and using a stack to track left brackets. It takes a user input, runs the matching method and outputs any errors or indicates if the brackets were successfully matched.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
87 views2 pages

Bracket Matching Assignment

The document describes an implementation of a bracket matching algorithm using a stack in Java. It defines methods to check if a bracket is left or right, if two brackets match, and performs the matching by iterating through the string and using a stack to track left brackets. It takes a user input, runs the matching method and outputs any errors or indicates if the brackets were successfully matched.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

NAME: ODEKUNLE OLUWATOBI ALAO MATRIC NO: 200868 COURSE: DATA STRUCTURES

IMPLEMENTATION OF BRACKET MATCHING ALGORITHM USING STACK IN JAVA.

import java.util.*;
public class BracketMatching {
static String input;
static String output = "";
public BracketMatching(String input){
this.input = input;
}
public static boolean checkBracketMatched(char leftBracket, char rightBracket){
boolean checkBracket = false;
if( (leftBracket == '(' && rightBracket == ')') || (leftBracket == '[' && rightBracket == ']') || (leftBracket ==
'{' && rightBracket == '}') ){
return true;
}
return checkBracket;
}
public static boolean checkLeftBracket(char current){
boolean check = false;
if( current == '{' || current == '[' || current == '(' ){
check = true;
}
return check;
}
public static boolean bracketMatched(){
int error = 0;
int currentPosition;
boolean check = false;
Stack <Character> stack = new Stack<Character>();
int last_position = input.length();
for(int position = 0; position < input.length(); position++){
char currentChar = input.charAt(position);
if( checkLeftBracket(currentChar) ){
stack.push(currentChar);
if( position == last_position-1 ){
output += "End of the string\n";
error += 1;
}
}
else{
if( stack.isEmpty() ){
output += "Error: no matching left for "+ currentChar + " at position "+(position+1) + ", the stack is
empty\n";
error += 1;
}
else{
char topStack = stack.peek();
if( checkBracketMatched(topStack,currentChar) ){
currentPosition = stack.size();
stack.pop();
output += "Bracket "+topStack+" at position "+(position) + " is matched with Bracket
"+currentChar+" at position "+(position+1)+"\n";
}
else{
output += "Error: no matching left bracket for "+currentChar + " at position "+(position+1)+"\n";
error += 1;
}
}
}
}
if(stack.isEmpty()){
check = true;
if(error == 0){
output += "Brackets successfully matched";
}
}else{
output += "Error: there are "+stack.size()+" left brackets in the stack without right brackets";
}
return check;
}
public static void main(String[] args) {
Scanner console = new Scanner(System.in);
System.out.println( " Enter any expression: ({[ ])} " );
String input = console.nextLine();
BracketMatching match = new BracketMatching(input);
if(bracketMatched()){
System.out.println(match.output);
}
else{
System.out.println(match.output +"\nBracket not matched");
}
}
}

OUTPUT:

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy