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

C++ Programming (C++14 standard) # Ifndef _MBRACKET_ # define _MBRACKET_ #includ

ID: 3880454 • Letter: C

Question

C++ Programming (C++14 standard)

# Ifndef _MBRACKET_
# define _MBRACKET_

#include <string>

using namespace std;

bool is_left(char c);

bool is_right(char c);

bool matches(char L, char R);

bool balanced(string str);

#endif

#include <iostream>
#include <stack>
#include <cassert>
#include "bracketMatcher.h"

string left_brackets("[({");
string right_brackets("])}");

bool is_left(char c)
{
return left_brackets.find(c) != string::npos;
}

bool is_right(char c)
{
return right_brackets.find(c) != string::npos;
}

bool matches(char L, char R)
{
assert(is_left(L) && is_right(R));
return left_brackets.find(L) == right_brackets.find(R);
}

bool balanced(string str)
{

// COMPLETE CODE HERE
stack<char> S;  

}

You are to write a function to determine if the brackets ([1 ) are properly nested, i. e., balanced. The function will have a string as a parameter that can contain bracket and other characters. If the brackets are properly nested, the function will print the message The brackets in your string are properly matched Otherwise, it will print a message like this: Bracket mismatch at index and right bracket> or Unmatched bracket at index

Explanation / Answer

#include <iostream>
#include <iostream>
#include <stack>
#include <cassert>
#include <string>
//Added this to convert string to char array.
#include<bits/stdc++.h>

using namespace std;

string left_brackets("[({");
string right_brackets("])}");

bool is_left(char c)
{
return left_brackets.find(c) != string::npos;
}
bool is_right(char c)
{
return right_brackets.find(c) != string::npos;
}
bool matches(char L, char R)
{
assert(is_left(L) && is_right(R));
return left_brackets.find(L) == right_brackets.find(R);
}

//Added this utitlity method
bool isMatching(stack<char>& stack, char openingBracket, char closingBracket)

{
//If stack empty, then matching error
if (stack.empty()){
std::cerr << "Unmatched " << closingBracket;
return true;
}

//Self explanatory
char top = stack.top();
stack.pop();
if (top != openingBracket){
if (top == '{')
cout << "Expected } but found " << closingBracket;
else if (top == '(')
cout << "Expected ) but found " << closingBracket;
else if (top == '[')
cout << "Expected ] but found " << closingBracket;
else
cout << "nvalid character" << top;
return true;
}
return false;
}

bool balanced(string str)
{
stack<char> stk;
int n = str.length();
char char_array[n+1];
//COnvert str to char array
strcpy(char_array, str.c_str());
//Check all the characters
for (int i =0; i<n; i++){
if (char_array[i] == '}'){
if (isMatching(stk, '{', '}')){
return EXIT_SUCCESS;
}
} else if (char_array[i] == ')'){
if (isMatching(stk, '(', ')')) {
return EXIT_SUCCESS;
}
}
else if (char_array[i] == ']') {
if (isMatching(stk, '[', ']')) {
return EXIT_SUCCESS;
}
} else{
stk.push(char_array[i]);
}
}
if(!stk.empty()){
cout << "Invalid characters. Some unmatched characters left in the stack ";
}else{
cout << "Brackets in your string are properly matched"
}
}

int main(){
balanced("[({}])");
}

Please let me know in case of any doubts.

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