Question

Please use Java: Balancing Grouping Symbols Write a method that takes a string parameter and determines...

Please use Java:

  1. Balancing Grouping Symbols

Write a method that takes a string parameter and determines whether the string contains matching grouping symbols. Grouping symbols are parenthesis ( ), curly braces { }, and brackets [].  For example, the string {a(b+ac)d} and  kab*cd contain matching grouping symbols. However, the strings ac)cd(e(k, xy{za(dx)k, and {a(b+ac)d} do not contain matching grouping symbols. (Note: open and closed grouping symbols have to match both in number and in the order they occur in the string).

Write a method that uses a stack data structure to determine whether its string parameter has matching grouping symbols. Write a program to test and demo your method.

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

CODE

// Java program for checking

// balanced Parenthesis

public class Main

{

static class stack

{

int top=-1;

char items[] = new char[100];

void push(char x)

{

if (top == 99)

{

System.out.println("Stack full");

}

else

{

items[++top] = x;

}

}

char pop()

{

if (top == -1)

{

System.out.println("Underflow error");

return '\0';

}

else

{

char element = items[top];

top--;

return element;

}

}

boolean isEmpty()

{

return (top == -1) ? true : false;

}

}

/* Returns true if character1 and character2

are matching left and right Parenthesis */

static boolean isMatchingPair(char character1, char character2)

{

if (character1 == '(' && character2 == ')')

return true;

else if (character1 == '{' && character2 == '}')

return true;

else if (character1 == '[' && character2 == ']')

return true;

else

return false;

}

/* Return true if expression has balanced

Parenthesis */

static boolean areParenthesisBalanced(char exp[])

{

/* Declare an empty character stack */

stack st=new stack();

/* Traverse the given expression to

check matching parenthesis */

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

{

/*If the exp[i] is a starting

parenthesis then push it*/

if (exp[i] == '{' || exp[i] == '(' || exp[i] == '[')

st.push(exp[i]);

/* If exp[i] is an ending parenthesis

then pop from stack and check if the

popped parenthesis is a matching pair*/

if (exp[i] == '}' || exp[i] == ')' || exp[i] == ']')

{

/* If we see an ending parenthesis without

a pair then return false*/

if (st.isEmpty())

{

return false;

}

/* Pop the top element from stack, if

it is not a pair parenthesis of character

then there is a mismatch. This happens for

expressions like {(}) */

else if ( !isMatchingPair(st.pop(), exp[i]) )

{

return false;

}

}

}

/* If there is something left in expression

then there is a starting parenthesis without

a closing parenthesis */

if (st.isEmpty())

return true; /*balanced*/

else

{ /*not balanced*/

return false;

}

}

/* UTILITY FUNCTIONS */

/*driver program to test above functions*/

public static void main(String[] args)

{

String exp1 = "{a(b+ac)d}";

if (areParenthesisBalanced(exp1.toCharArray()))

System.out.println("Balanced ");

else

System.out.println("Not Balanced ");

String exp2 = "ac)cd(e(k, xy{za(dx)k";

if (areParenthesisBalanced(exp2.toCharArray()))

System.out.println("Balanced ");

else

System.out.println("Not Balanced ");

}

}

OUTPUT

Balanced

Not Balanced

Add a comment
Know the answer?
Add Answer to:
Please use Java: Balancing Grouping Symbols Write a method that takes a string parameter and determines...
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
  • Please use Java. Write a non-recursive method that uses a stack to check whether its string...

    Please use Java. Write a non-recursive method that uses a stack to check whether its string parameter is a palindrome. Write a program to test and demo your method. Fibonacci Numbers Write a method that uses a stack to determine the desired Fibonacci number. Write a program to test and demo your method. Balancing Grouping Symbols Write a method that takes a string parameter and determines whether the string contains matching grouping symbols. Grouping symbols are parenthesis ( ), curly...

  • Please give me that correct code for this by using C++ language. and i hope you...

    Please give me that correct code for this by using C++ language. and i hope you use Visual stduio Write a function that takes a string parameter and determines whether the string contains matching grouping symbols. Grouping symbols are parenthesis ( ) , brackets [] and curly braces { }.  For example, the string  {a(b+ac)d[xy]g} and  kab*cd contain matching grouping symbols. However, the strings ac)cd(e(k, xy{za(dx)k, and {a(b+ac}d) do not contain matching grouping symbols. (Note: open and closed grouping symbols have to match...

  • Write a function that takes a string parameter and determines whether the string contains matching grouping...

    Write a function that takes a string parameter and determines whether the string contains matching grouping symbols. Grouping symbols are parenthesis ( ) , brackets [] and curly braces { }. For example, the string {a(b+ac)d[xy]g} and kab*cd contain matching grouping symbols. However, the strings ac)cd(e(k, xy{za(dx)k, and {a(b+ac}d) do not contain matching grouping symbols. (Note: open and closed grouping symbols have to match both in number and in the order they occur in the string). Your function must use...

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
ADVERTISEMENT