Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

10. Write a function that loops over a string, character by character, and check

ID: 3729672 • Letter: 1

Question

10. Write a function that loops over a string, character by character, and checks it for balanced curly braces ( er that . or a character is a left symbol, push it onto a stack. If it is a right symbol, compare it to the symbol at top of the stack. If they correspond, the left signal has been matched and can be popped. If they don't parentheses 0, and square brackets |. The program should ignore any character that is not a , , ) correspond, the left symbol has not been closed correctly and the function should return false. bool bracketsMatch(const std: :stringk str) (

Explanation / Answer

Hello, I have a solution for you. Implemented everything as per the requirements. Defined following things in this answer.

//main.cpp

#include<iostream>

#include<stack>

using namespace std;

//method to check if the brackets are balanced in a string

bool bracketsMatch(const string &str){

                //defining a stack of characters

                stack<char> bracketsStack;

                //looping through the string

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

                                //getting character at current position

                                char c=str[i];

                                if(c=='{' || c=='[' || c=='('){

                                                //pushing to the stack, if the character is any opening bracket

                                                bracketsStack.push(c);

                                }

                                //checking if the character is any closing bracket

                                if(c=='}'){

                                                if(bracketsStack.empty()){

                                                                //that means there is no opening brace found, returning false

                                                                return false;

                                                }

                                                //getting the character at the top of the stack

                                                char tmp=bracketsStack.top();

                                                //if it is not the opening bracket '{' character, returning false

                                                if(tmp!='{'){

                                                                return false;

                                                }else{

                                                                //removing the top element (string is balanced so far)

                                                                bracketsStack.pop();

                                                }

                                }

                                //performing similar checks in other two braces too

                                if(c==']'){

                                                if(bracketsStack.empty()){

                                                                return false;

                                                }

                                                char tmp=bracketsStack.top();

                                                if(tmp!='['){

                                                                return false;

                                                }else{

                                                                bracketsStack.pop();

                                                }

                                }if(c==')'){

                                                if(bracketsStack.empty()){

                                                                return false;

                                                }

                                                char tmp=bracketsStack.top();

                                                if(tmp!='('){

                                                                return false;

                                                }else{

                                                                bracketsStack.pop();

                                                }

                                }

                }

                //at the end, if the stack is empty, brackets are balanced, else not

                if(bracketsStack.empty()){

                                return true;

                }else{

                                return false;      

                }

               

}

int main(){

                string text;

                cout<<"Enter an expression: ";

                /*getting text, note that the white spaces are not allowed using cin>> input, if any

                white spaces are found, the system will consider only the characters before the white space */

                cin>>text;

                //checking

                if(bracketsMatch(text)){

                                cout<<"brackets are balanced"<<endl;

                }else{

                                cout<<"brackets are not balanced"<<endl;

                }

                return 0;

}

/*OUTPUT1*/

Enter an expression: (hello){[world]}

brackets are balanced

/*OUTPUT2*/

Enter an expression: }hey{howareyou)

brackets are not balanced

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote