Hello everyone.
This question is from the book "Jumping into C++", chapter 19. I have removed "<" from HTML tags, so that the whole question is visible.
Write a program that reads in HTML text that the user types in (don’t worry, we’ll cover how to
read from a file later). It should support the following HTML tags: html>, head>, body>, b>,
i>, and a>. Each HTML tag has an open tag, e.g. html>, and a closing tag which has a forwardslash
at the start: /html>. Inside the tag is text that is controlled by that tag: b>text to be
bolded/b> or i>text to be italicized/i>. The head> /head> tags control text that is
metadata, and the body> /body> tags surround text that is to be displayed. a> tags are used
for hyperlinks, and have an URL in the following format: <a href=URLtext /a>.
Once your program has read in some HTML, it should simply ignore html>. It should remove
any text from the head> section so that it doesn't show up when you output it. It should then
display all text in the body, modifying it so that any text between b> and /b> will show up
with asterisks (*) around it, any text inside i> and /i> will show up with underscores (_)
around it, and any text with a <a href=linkurl link text /a> tag shows up as link text (linkurl).
--------------------------------------------------------------------------------------------------
Brief explanation of my attempt -
Step 1 - assigned the string between body tags to a new string variable "body".
Step 2 - created a map "check" and inserted parts of the string in "body" leaving out the string between b> /b> tags.
Step 3 - tried to replaced the inserted elements of map "check" which contain i> i/> tags, by first assigning the values of itr->first and itr->second to new variables, then deleting that element and inserting new elements in map after removing string between i> /i> tags.
**This is where I face the problem. The code doesn't give an error but the program doesn't return 0. If I remove the "check.erase(index)", it works, but that doesn't solve the purpose.**
I tried doing a similar thing in a separate code and it worked. I have included the other code after the following one, which is the attempted solution to the question above.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
|
int main()
{
map<int,string>check;
string x = "<html>";
string xx = "</html>";
string y = "<head>";
string yy = "</head>";
string z = "<body>";
string zz = "</body>";
string a = "<b>";
string aa = "</b>";
string b = "<i>";
string bb = "</i>";
string c = "<a href";
string ccc = ">";
string cc = "</a>";
string type = "<html>\n<head>I dont know what to write here</head>\n<body>\ngurasees is <b>my</b> good <i>name</i>. You can find <b>me</b> on the <i>web</i>. Link is <a href = www.google.com>gura</a>\n</body>\n</html>";
int f = type.find(z)+z.size()+1;
int g = type.find(zz)-f;
string body = type.substr(f,g);
map<int,string>::iterator itr;
map<int,string>::iterator ends = check.end();
//--------------------------STEP 2-----------------------------------------
check.insert ({body.find(body[0]), body.substr (body.find(body[0]), body.find(a,0)-1)});
int k = body.find(a, 0);
k++;
int p = body.find(aa,0);
for (int i = body.find(a,k); i != string::npos && p != string::npos; i = body.find(a, i), p = body.find(aa, p))
{
check.insert({p+aa.size(),body.substr(p+aa.size(), i-1-(p+aa.size()))});
i++; p++;
}
check.insert({p + aa.size() , body.substr(p + aa.size() , body.size() - 1 - p ) } ) ;
//------------------------STEP 3-------------------------------------------
for (itr = check.begin(); itr != ends; itr++)
{
cout << itr->second.find(b) << endl; //prints out correctly
if (itr->second.find(b) != string::npos)
{
int index = itr->first;
string italic_component = itr->second;
check.erase(index);
check.insert ({index, italic_component.substr(italic_component.find(italic_component[0]), italic_component.find(b, 0)-1)});
int k = italic_component.find(b, 0);
k++;
int j = italic_component.find(bb, 0);
for (int i = italic_component.find(b, k); i != string::npos && j != string::npos; i = italic_component.find(b, k), j = italic_component.find(bb, j) )
{
check.insert({index + j + bb.size() , italic_component.substr(j + bb.size(), i - 1 - (j+bb.size()))});
i++; j++ ;
}
check.insert({j + index + bb.size() ,italic_component.substr(j + bb.size() , italic_component.size() - 1 - j ) } ) ;
}
}
//------------------------------STEP 4-------------------------------------
for ( int i = body.find( a , 0 ), j = body.find( aa , 0), k = body.find( b , 0 ), l = body.find( bb , 0); i != string::npos && j != string::npos && j != string::npos && k != string::npos; i = body.find(a, i ), j = body.find(aa,j), k = body.find(b, k), l = body.find(bb,l) )
{
check.insert( {i, "*" + body.substr(i+a.size(), j-(i+a.size())) + "*" } );
check.insert( {k, "_" + body.substr(k+b.size(), l-(k+b.size())) + "_" } );
i++; j++; k++; l++;
}
}
|
-------------------------THE OTHER CODE--------------------------------
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
|
int main()
{
map<int,string>check;
check[10] = "gurasees is there for you";
check[20] = "aka is not born yet";
check[30] = "sam please wait for years";
map<int,string>::iterator itr;
map<int,string>::iterator ends = check.end();
for(itr = check.begin(); itr!= ends; itr++)
{
if(itr->second.find("born") != string::npos)
{
int index = itr->first;
string italic = itr->second;
check.erase(itr->first);
check.insert({index, italic.substr(italic.find(italic[0]), 5)});
}
}
|