Revise the program such that it handles four different kinds of delimiters: {},
ID: 3640585 • Letter: R
Question
Revise the program such that it handles four different kindsof delimiters: {}, (), [], and <>.
Note that we define the string to be balanced if: each
left delimiter is matched by its corresponding right
delimiter, which itself is properly nested. For example,
the string {a [b(c <d>)] e f g} is balanced. The string (a]
is not balanced. ([a b)] is not balanced either because the
nesting is incorrect. In general, a string such as
(...[...)...] is not balanced.
HINT: You must expand the items you push on the stack,
and your "popping" must be more selective. That is,
just because you had something on the stack to pop
does not mean that you actually have a match. For
example, if you are currently looking at a "}" and you
pop a "(", then your string is *not* balanced. (In fact,
it's "right heavy.")
Thank You!!!
//////////////////////////////////////////////////////////
//
// This program determines whether a given string
// has balanced braces ("{}").
//
// Author: Mohammad
//
//////////////////////////////////////////////////////////
import java.util.*;
public class balanced
{
public static void main(String[] args)
{
Stack<Character> the_stack;
String aString;
boolean right_heavy;
int i;
Scanner sc = new Scanner(System.in);
the_stack = new Stack<Character>();
System.out.print("Enter a string: ");
aString = sc.nextLine();
right_heavy = false;
i = 0;
// Scan the string char-by-char (first-to-last)
// until we've visited all the chars OR we detect
// a "right heavy" condition
while(i < aString.length() && !right_heavy)
{
// push a '{' on the stack
if(aString.charAt(i) == '{')
the_stack.push('{');
// when we see a '}', try to pop. If unsuccessful,
// then we have a right heavy condition
else if(aString.charAt(i) == '}')
{
try
{
the_stack.pop();
}
catch(EmptyStackException e)
{
right_heavy = true;
}
}
// simply skip chars other than '{' and '}'
i++;
} // end while
// The string is balanced if not "right heavy" and
// not "left heavy."
//
// NOTE: "not 'left heavy'" <==> stack empty
if((!right_heavy) && the_stack.isEmpty())
System.out.println("The string "" +
aString +
"" has balanced braces.");
else
System.out.println("The string "" +
aString +
"" does NOT have balanced braces.");
} // end main
}
Explanation / Answer
import java.util.EmptyStackException;
import java.util.Scanner;
import java.util.Stack;
public class BalancingParanthesis {
public static void main(String[] args)
{
Stack<Character> the_stack;
String aString;
boolean right_heavy;
int i;
//{}, (), [], and <>.
String openBrackets = "{([<";
String closeBrackets = "})]>";
Scanner sc = new Scanner(System.in);
the_stack = new Stack<>();
System.out.print("Enter a string: ");
aString = sc.nextLine();
right_heavy = false;
i = 0;
// Scan the string char-by-char (first-to-last)
// until we've visited all the chars OR we detect
// a "right heavy" condition
while(i < aString.length() && !right_heavy)
{
for (int b = 0; b < openBrackets.length(); b++)
{
// push a open bracket on the stack
if(aString.charAt(i) == openBrackets.charAt(b))
the_stack.push(aString.charAt(i));
// when we see a close bracket, try to pop.
// If unsuccessful or not correct open bracket,
// then we have a right heavy condition
else if(aString.charAt(i) == closeBrackets.charAt(b))
{
try
{
char bracket = the_stack.pop();
if (openBrackets.indexOf(String.valueOf(bracket)) != b)
{
right_heavy = true;
}
}
catch(EmptyStackException e)
{
right_heavy = true;
}
}
}
// simply skip chars other than open brackets and close brackets
i++;
} // end while
// The string is balanced if not "right heavy" and
// not "left heavy."
//
// NOTE: "not 'left heavy'" <==> stack empty
if((!right_heavy) && the_stack.isEmpty())
System.out.println("The string "" + aString + "" has balanced braces.");
else
System.out.println("The string "" + aString + "" does NOT have balanced braces.");
} // end main
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.