In: Computer Science
We want to design and develop a text statistical analyzer. This analyzer should consider an input text as a finite sequence of characters, including letters (‘a’, .., ‘z’, ‘A’, ..., ‘Z’), digits (0,…, 9), special characters (space and new line), and punctuation characters (‘.’, ‘,’, ‘;’). The input text must end with ‘#’. A word is defined as a sequence of alphanumeric characters (letters and numbers, 0 to 9) delimited by two separators. A separator is any non-alphanumeric character. Use your favorite object-oriented programming language to write a program that accepts a text as input and gives as output the following results: i. The total number of alphanumeric characters (i.e., letters and digits). ii. The total number of letters and their frequency with respect to the total number of alphanumeric characters. 2 iii. The total number of digits and their frequency with respect to the total number of alphanumeric characters. iv. The total number of words. v. The total number of words starting with “ted” and their frequency with respect to the total number of words. vi. The total number of words ending with “ted” and their frequency with respect to the total number of words. vii. The total number of words having “ted” in the middle and their frequency with respect to the total number of words. viii. The total number of sequences of “ted” and their frequency with respect to the total number of sequences of three alphanumeric characters. The implementation of this text statistical analyzer should be as structured as possible.
Write a code for the above problem either in c/c++.
#include <iostream> #include <fstream> #include <cstdlib> #include <cctype> using namespace std; void countStuff(istream& in, int& chars, int& words, int& lines) { char cur = '\0'; char last = '\0'; chars = words = lines = 0; while (in.get(cur)) { if (cur == '\n' || (cur == '\f' && last == '\r')) lines++; else chars++; if (!std::isalnum(cur) && // This is the end of a std::isalnum(last)) // word words++; last = cur; } if (chars > 0) { // Adjust word and line if (std::isalnum(last)) // counts for special words++; // case lines++; } } int main(int argc, char** argv) { if (argc < 2) return(EXIT_FAILURE); ifstream in(argv[1]); if (!in) exit(EXIT_FAILURE); int c, w, l; countStuff(in, c, w, l); 1 cout << "chars: " << c << '\n'; cout << "words: " << w << '\n'; cout << "lines: " << l << '\n'; }