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

#include<iostream> #include<stack> #include<string> using namespace std; string

ID: 3699575 • Letter: #

Question

#include<iostream>
#include<stack>
#include<string>
using namespace std;

string xml(string shuru);

int main() {
string input;
cout << "Enter the input" << endl;
getline(cin, input);
cout << "Output:" <<xml(input) << endl;
}

string xml(string shuru) {
char token ,topToken;                
stack<char>opStack;   
string out;   
const string BLANK = " ";
for (int i = 0; i < opStack.size(); i++) {
  token = shuru[i];
  switch (token)
  {
  case '<': opStack.push(token);
   break;
  case '>': for (;;)
  {
   if (!opStack.empty()) {
    topToken = opStack.top();
    opStack.pop();
    if (topToken == '<') break;
    out.append(BLANK + topToken);
   }
  }
      break;
  case '</ ': opStack.push(token);
  default:
   for (;;)
   {
    if (!isalnum(shuru[i + 1])) break;
    i++;
    token = shuru[i];
    out.append(1, token);
   }
  }
}
return out;
}

Help me fix this code(c++) so it can:
1. Readthe input until the beginning of a tag is detected. (i.e. tagsbegin with <: if the next character is a / (slash), then it isan end-tag; otherwise it is a start-tag).

2. Readthe tag's identity. (e.g. both tags <x> and </x> havethe same identity: 'x').

3. Ifthe tag was a start-tag, push it onto the Stack.

4.Otherwise, it is an end-tag. In this case, pop the Stack (whichcontains a previously pushed start-tag identity) and verify thatthe popped identity is the same as the the end-tag just processed.Note: if the stack cannot be popped (because it isempty), the input is invalid; the algorithm should STOP.

5. Ifthe identities do not match, the XML expression isinvalid. STOP.

6. Ifthey do match, then no error has been detected (yet!).

7. Ifthere is still input that has not yet been processed, go back tothe first step.

8. Otherwise (no moreinput) then the input is valid unless the Stack isnot empty. Indicate whether theinput is valid (Stack empty) or invalid and STOP.

Example of XML tags:
XML

Valid?

Explanation

<a></a>

Yes

"a" tags balance

<a><b></b></a>

Yes

"a" outer tags and "b" inner tags balance

<a><b></a></b>

No

"a" end-tags does not match start-tag ("b")

<a><b><a></a><b></b></b></a>

Yes

all tags balance

<able><baker></Baker></able>

No

"Baker" end-tag does not matchstart-tag ("baker") (i.e. the tag names arecase-sensitive.)

Explanation / Answer

xml_validation.cpp

#include <iostream>
#include<stack>
#include<string>

using namespace std;

string xml(string shuru);

int main() {
string input;
cout << "Enter the input" << endl;
getline(cin, input);
cout << "Output:" <<xml(input) << endl;
}

string xml(string shuru) {
char token ,topToken;   
stack<char>opStack;
string out;

for (int i = 0; i < shuru.length(); i++) {
token = shuru[i];

if (token == '<')
{
if (shuru[i+1] != '/')
{
opStack.push(shuru[i+1]);

}
else
{
if(opStack.empty())
{
out = "INVAILD";
break;
}
else
{
topToken = opStack.top();
if(topToken == shuru[i+2])
{
out = "VALID";
opStack.pop();
}
else
{
out = "INVAILD";
break;
}
}
}
}
}
if(!opStack.empty()){
out = "INVALID";
}
return out;
}