Question

In: Computer Science

This is C++ Create a program that reads an HTML file and converts it to plain...

This is C++

Create a program that reads an HTML file and converts it to plain text.


Console:


HTML Converter

Grocery List
* Eggs
* Milk
* Butter


Specifications:
The HTML file named groceries.html contains these HTML tags:


<h1>Grocery List</h1>
<ul>
<li>Eggs</li>
<li>Milk</li>
<li>Butter</li>
</ul>


When the program starts, it should read the contents of the file, remove the HTML tags, remove any spaces to the left of the tags, add asterisks (*) before the list items, and display the content and the HTML tags on the console as shown above.


Note
The groceries.html file ends each line with a carriage return character (‘\r’), not a new line character (‘\n’). To account for that, when you read the file, you can use the third parameter of the getline() function to specify the end of the line. For more information, you can search online and check the documentation of the getline() function.

Solutions

Expert Solution

Here is the solution,

my platform : Windows

IDE : Code Blocks

please keep your groceries.html file in the same folder along with the program

the program runs with any basic html file(code)

// your code starts here

#include <iostream>
#include<fstream>
using namespace std;

int main()
{
string line;
fstream myfile;
char array[5000]; // declaring an array for storing the plain text

// opening the file "groceries.html". please keep the file in the same folder along with the program
myfile.open("groceries.html", ios::in);
if (!myfile)
cout << "file cannot open!";

//-----------------------------------------

int k=0; // variable to store characters in the array

// reading line by line using the getline function
while (getline(myfile, line,'\r'))
{
// reading every character of every line
for(unsigned int i = 0; i<line.length(); i++) {
// checking for the character '<'
if(line[i]=='<' ) {
// check if the charcter 'L' and 'I' follows after the '<'
// checking for i+2 < line.length , in case it goes out of index
if((i+2) < line.length()){
if((line[i+1]=='l' || line[i+1]=='L' ) && (line[i+2]=='i' || line[i+2]=='I')) {
//if condition is satisfied store the '*' character in the array.
array[k++]='*';
}
}
// skip till the '>' character is encountered
while((i++) < line.length() && line[i]!='>'); // just a single line . there is block for this while loop
}

// check if the next character is '<' if so don't store it in the array
// also checking for (i+1) index in case it goes out of index
if((i+1)< line.length() && line[i+1]!='<')
array[k++]=line[i+1];
}
cout << '\n';
}
array[k] ='\0';
//------------------------------------------------
k=0; // assigning k to zero again

// display the contents of the array.
while(array[k]!='\0') {

cout<<array[k++];
}

return 0;
}

// code ends here

The code is running sucessfully and producing perfect output

my groceris.html file

here is the output

you can see that at the end it has also displayed the text "this is a header" .

i had added one more tag at the end of the html file(see the screenshot) for testing purpose.

you can use any basic html file and it will convert it into plain text.

also my max array size is 5000 characters.

Thank You.


Related Solutions

How to do in C++ HTML Converter Create a program that reads an HTML file and...
How to do in C++ HTML Converter Create a program that reads an HTML file and converts it to plain text. Console HTML Converter Grocery List * Eggs * Milk * Butter Specifications Your instructor should provide an HTML file named groceries.html that contains these HTML tags: <h1>Grocery List</h1> <ul>     <li>Eggs</li>     <li>Milk</li>     <li>Butter</li> </ul> When the program starts, it should read the contents of the file, remove the HTML tags, remove any spaces to the left of the tags, add...
Create a C Program that reads a file and replaces every other letter with an asterisk....
Create a C Program that reads a file and replaces every other letter with an asterisk. The first integer 'num' is the number of lines. Include an "Error" Message and exit the program if:    The wrong name for the input/output file is given    The input/output file can not be opened input.txt 7 Never Have We Ever Played A Game output.txt 7 N*v*r *a*e W* *v*r *l*y*d * G*m*
C++ Write a program that prompts for a file name and then reads the file to...
C++ Write a program that prompts for a file name and then reads the file to check for balanced curly braces, {; parentheses, (); and square brackets, []. Use a stack to store the most recent unmatched left symbol. The program should ignore any character that is not a parenthesis, curly brace, or square bracket. Note that proper nesting is required. For instance, [a(b]c) is invalid. Display the line number the error occurred on. These are a few of the...
C++ Assignment Hi, I need to create a program that: 1.Reads a source file (.txt) with...
C++ Assignment Hi, I need to create a program that: 1.Reads a source file (.txt) with following information: 1,2,3,4,5 red,blue,green,yellow,orange left, right,front, back 2. After having program read the .txt file, output the above information in categories of Symbol, Token Type, and Count : Example: Symbol---Token Type (data type)----Count (how many times symbol appeared in .txt file) =========================================================================== 1 ----digit ----1 2 ----digit ----1 red ----color ----1 blue ----color ----1 left ----direction ----1 right ----direction    ----1
C++ Assignment Hi, I need to create a program that: 1.Reads a source file (.txt) with...
C++ Assignment Hi, I need to create a program that: 1.Reads a source file (.txt) with following information: 1,2,3,4,5 red,blue,green,yellow,orange left, right,front, back 2. After having program read the .txt file, output the above information in categories of Symbol, Token Type, and Count : Example: Symbol---Token Type (data type)----Count (how many times symbol appeared in .txt file) =========================================================================== 1 ----digit ----1 2 ----digit ----1 red ----color ----1 blue ----color ----1 left ----direction ----1 right ----direction    ----1
C++ Assignment Hi, I need to create a program that: 1.Reads a source file (.txt) with...
C++ Assignment Hi, I need to create a program that: 1.Reads a source file (.txt) with following information: 1,2,3,4,5 red,blue,green,yellow,orange left, right,front, back 2. After having program read the .txt file, output the above information in categories of Symbol, Token Type, and Count : Example: Symbol---Token Type (data type)----Count (how many times symbol appeared in .txt file) =========================================================================== 1 ----digit ----1 2 ----digit ----1 red ----color ----1 blue ----color ----1 left ----direction ----1 right ----direction    ----1
c++ language Create a file program that reads an int type Array size 10; the array...
c++ language Create a file program that reads an int type Array size 10; the array has already 10 numbers, but your job is to resize the array, copy old elements of array to the new one and make it user input and add an additional 5 slots in the array, and lastly do binary search based on user input. close the file.
Create a Python program that: Reads the content of a file (Vehlist.txt) The file contains matching...
Create a Python program that: Reads the content of a file (Vehlist.txt) The file contains matching pairs of vehicle models and their respective makes Separate out the individual make and model on each line of the file Add the vehicle make to one list, and the vehicle model to another list; such that they are in the same relative position in each list Prompt the user to enter a vehicle model Search the list containing the vehicle models for a...
Write a C program that Reads a text file(any file)  and writes it to a binary file....
Write a C program that Reads a text file(any file)  and writes it to a binary file. Reads the binary file and converts it to a text file.
Create a program that reads a file of 2D coordinates and calculates the bounding box and...
Create a program that reads a file of 2D coordinates and calculates the bounding box and center of the bounding box. The bounding box is defined as the minimum area that fully encompasses all the coordinates. The center of that bounding box is calculated by taking the mean of the bounding box coordinates: ( x1+x2 2 , y1+y2 2 ). • If the input file cannot be opened, warn the user by printing "CANNOT OPEN FILE." to stdout. • Print...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT