how to count characters inside tags in a html file?
Feb 19, 2014 at 8:18pm UTC
I want to be able to count the characters inside the tags in the html file. I can get the total of the whole file characters but not the total of the characters inside the tag. how could I do this? here is what I have so far
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 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
#include <iostream> // include standard I/O libraries
#include <iomanip>
#include <fstream>
#include <string>
using namespace std;
int main ()
{
int numlines = 0, // counts the number of lines
linkcount = 0, // counts the number of links
tagcount = 0, // counts the number of tags
comment = 0, //counts hte nuber of comments
numchars = 0, //counts the number of characters
numtagchars = 0;
ifstream inFile; //streams file name
string filename, //input for the file name
line; //reads the lines in the file
bool inTag = false ;
const char openTag = '<' ,
closeTag = '>' ;
char ch;
//displays header
cout << "=========================================================================\n"
<< "HTML File Analyzer\n"
<< "=========================================================================\n"
<< endl
<< "Enter a valid filename (No blanks!): " ;
cin >> filename;
inFile.open(filename.c_str());
//while the incorrect name is entered
while (!inFile){
cout << "Re-enter a valid filename (No blanks!): " ;
cin >> filename;
inFile.open(filename.c_str());
}
cout << endl
<< "=========================================================================\n"
<< "Text of the file\n"
<< "=========================================================================\n"
<< endl;
//diplays the file text
while (getline(inFile, line)){
cout << line << endl;
numlines++;
//counts the tags in the file
for (unsigned int tag = 0; tag < line.length(); tag++){
if (line.at(tag) == '<' ){
tagcount++;
}
}
// counts the comments in the file
for (unsigned int com = 0; com < line.length(); com++){
if (line.at(com) == '<' ){
if (line.at(com + 1) == '!' ){
comment++;
}
}
}
//counts the links in the file
for (unsigned int lin = 0; lin < line.length(); lin++){
if (line.at(lin) == '<' ){
if (line.at(lin + 1) == 'l' ){
if (line.at(lin + 2) == 'i' ){
linkcount++;
}
}
}
}
//counts the chars in the file
for (unsigned int chars = 0; chars < line.length(); chars++){
if (line.at(chars)){
numchars++;
}
}
//No clue how to do it here//
/*for (unsigned int tagchars = 0; tagchars < line.length(); tagchars++){
if(line.at(tagchars) == '<'){
numtagchars++;
}
}*/
}
cout << endl
<< "=========================================================================\n"
<< "End of the text\n"
<< "=========================================================================\n"
<< endl;
cout << "Analysis of file"
<< endl
<< "----------------"
<< endl
<< endl;
//displays summary of file
numchars+=numlines;
cout << "Number of lines: " << numlines << endl
<< "Number of tags: " << tagcount << endl
<< "Number of comments: " << comment << endl
<< "Number of links: " << linkcount << endl
<< "num of chars " << numchars << endl
<< "num of tagchars " << numtagchars << endl;
cout << endl;
inFile.clear();
inFile.close ();
cout << endl
<< "=========================================================================\n"
<< "Thanks for using the HTML File Analyzer\n"
<< "=========================================================================\n"
<< endl;
return (0);
}
Topic archived. No new replies allowed.