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

Write a C + + program that checks whether the bracketing operators (parentheses,

ID: 3631711 • Letter: W

Question

Write a C + + program that checks whether the bracketing operators (parentheses, brackets, and curly braces) in a string are properly matched. As an example of proper matching, consider the string { s = 2 * (a [2] + 3 ); x = (1 + (2)); ) If you go through the string carefully, you discover that all the bracketing operators are correctly nested, which each open parenthesis matched by a close parenthesis, each open bracket matched by a close bracket, and so on. On the other hand, the following strings are all unbalanced for reasons indicated: ({[]) The line is missing a close parenthesis. ){ The close parenthesis comes before the open parenthesis. {{}} The bracketing operators are improperly nested.

Explanation / Answer

please rate - thanks

#include<iostream>
using namespace std;
char pop(char[], int& );
void push(char[],int&,int, char) ;
bool checkinput(string,int,char[]);
int main()
{ int maxsize=20;
char stack[maxsize];
string input;
bool good;
cout<<"Enter the string who's parenthesis you want checked: ";
getline(cin,input);
good=checkinput(input,maxsize,stack);
if(good)
    cout<<"valid parenthesis ";
else
     cout<<"invalid parenthesis ";
system("pause");
return 0;
}
bool checkinput(string input,int maxsize,char stack[])
{char m=' ';
char opens[3]={'[','{','('};
char closes[3]={']','}',')'};
int n=0,i=0,j,k;
while(i<input.size())
   {
   for(j=0;j<3;j++)
    {
    if(input[i]==opens[j])
        {push(stack,n,maxsize,input[i]);
       // j=3;
        }       
    else
       if(input[i]==closes[j])
           { m=pop(stack,n);

            if(m!=opens[j])
                 return false;
           }
     }
    i++;
    }
if(n!=0)
     return false;
return true;
}   
char pop(char a[], int& n)
{ if (n <= 0)
        return 'x';
   n--;
   return a[n];
}
void push(char a[], int& n, int maxsize, char val)
{if (n >= maxsize)
        {cout<<"Stack overflow - program aborted ";
          system("pause");
          system("exit");
          }
   a[n] = val;
   n++;
   return;
}

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