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.

1
  • 2
    Not a good idea to include an image of text. Cannot be searched nor useful on a mobile phone Commented Dec 3, 2015 at 19:05

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

Comments

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.

2 Comments

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.
@mmoment as I understand he is currently learning so it is makes sense to implement a basic parser in order to understand inner mechanisms.
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";
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.