Starting Out With C++: Early Objects (10th Edition)
Starting Out With C++: Early Objects (10th Edition)
10th Edition
ISBN: 9780135235003
Author: Tony Gaddis, Judy Walters, Godfrey Muganda
Publisher: PEARSON
Question
Book Icon
Chapter 18, Problem 10PC
Program Plan Intro

Balanced Parentheses

Program Plan:

  • Include required header files
  • Declare function prototype
  • Inside “main ()” function,
    • Declare a variable “strng”.
    • Get a string from the user.
    • Check if the Boolean function “is_Balanced ()” returns true.
      • If the condition is true then the string has balanced parentheses.
      • If the condition is not true then the string does not have balanced parentheses.
  • In “is_Balanced ()” function,
    • Declare a Boolean variable “status”.
    • Create an object for stack.
    • Use for loop to step through each character in a string.
      • Use Switch…Case structure check the character has set of parentheses or not.
        • If left parenthesis is detected,
          • Push it into the stack using the function “push ()”.
        • If right parenthesis is detected,
          • Check if the stack is empty using the function “empty()”.
            • If the stack is empty then assign “false”
            • If the stack is not empty then assign “true”
    • Check if the stack is empty and assign “true”. Else, assign “false”.
    • Return the variable “status”.

Blurred answer
Students have asked these similar questions
A set of instructions may be used to show a function's return address. Remember that any modifications you make to the stack cannot prevent the procedure from returning to its caller.
Create a  C code that will allow the user to input a word and check whether it is a palindrome or not. Use a stack to reverse the order of a string and identify whether it is a palindrome or not. It should not be case-sensitive. Example 1: Input string: Racecar Reversed string: RACECAR (Note, this can also be all lowercase) Palindrome: Yes exampl 2: Input string:caRrot Reversed string: TORRAC(Note, this can also be all lowercase) Palindrome: No   Note: maximum string length is 20
7. The following function converts a postfix expression to an infix expression assuming the expression can only contain unsigned integer numbers and +' and -' operators. 1. public static String convert (String postfix) { 2. String operand1, operand2, infix; 3. Stack s = new AStack; 4. int i = 0; 5. while (i < postfix.length) { char nextc = postfix.charAt (i); if ((nextc 6. 7. '+')|| (nextc == '-') { == operand2 = s.pop (); operandl = s.pop () ; infix = '(' + operandl + nextc + operand2 + ')'; s.push (infix); i++; 8. 9. 10. 11. } 12. else if (Character.isDigit (nextc)){ int start = i; while (Character.isDigit (postfix.charAt (i)) i++; infix = postfix.substring (start, i); s.push (infix); 13. 14. 15. 16. 17. } else i++; 18. 19. } 20 return s.pop () ; 21. } (a) Rewrite only the lines of code that need to be changed so that the above function converts a postfix expression to a prefix expression. (b) Describe in words what changes are needed on the above given convert () function so that it…
Knowledge Booster
Background pattern image
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
Programming Logic & Design Comprehensive
Computer Science
ISBN:9781337669405
Author:FARRELL
Publisher:Cengage
Text book image
C++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
Publisher:Cengage Learning