1

I am currently reading book from Allex Allain and there is practice problem: enter image description here

And i dont know how to think about this problem and i am somehow stuck , should i first find every tag and save it to array or vector ? and then compare tags from vector with original string ? and make som conditions ? I dont looking for code from you i want to solve it by myself i am just looking for inspiration or some ideas , or some useful methods i could use. thank you.

Filip Jerga
  • 75
  • 1
  • 8

3 Answers3

1

You should make a Parser.

Read each word and if you find tag then find the next <tag>. If this is the opposite tag </tag> then you could create object from that tag.

Solution proposed imply to create interface named tag and derived class named <html>, <head>.

So in final you will have a motor (parser) which eats text and produces object.

Mr Lister
  • 45,515
  • 15
  • 108
  • 150
MSD561
  • 512
  • 5
  • 16
0

Yes like @MSD561 he could write a parser. Either from scratch and reinvent the wheel, or use a library.

An XML-library can be used to achieve the second and get a better understanding for the structures:

What XML parser should I use in C++?

It will also provide you with all the entries for the tags etc and you'll just have to parse through the xml tree.

Community
  • 1
  • 1
mmoment
  • 1,269
  • 14
  • 30
  • 2
    HTML is not guaranteed to be valid XML -- only XHTML is. If you want to use an XML parser on HTML you need to run it through a converter first, such as tidylib. – MrEricSir Dec 03 '15 at 19:47
  • @mmoment as I understand he is currently learning so it is makes sense to implement a basic parser in order to understand inner mechanisms. – MSD561 Jan 31 '19 at 19:09
0
using namespace std;

void main ()
{
    ifstream x; 
    string name,head="<html><head></head><body><table>", tail="</table></body></html>", bodystart="<tr><td>",bodyclose="</td></tr>";
    ofstream y;
    x.open("example.txt");
    y.open("myhtmlfile.html");
    y<<head;
    while(!x.eof())
    {
        x>>name; 
        y<<bodystart<<name<<bodyclose;
    }
    y<<tail;

    x.close();
    cout<<"\n\n";
}
pacholik
  • 8,607
  • 9
  • 43
  • 55
Mr. Kumar
  • 26
  • 2