Write a C program to count words and numbers in a plain text
Write a C++ program to count words and numbers in a plain text file. Words and numbers can be repeated. You can use any algorithm and data structures that you prefer, as long as the results are correct. It is preferred, but not necessary, that your algorithm is as efficient as possible, both in processing time as well as memory management.
The input is one text file, with words and integers, separated by any other symbols including spaces, commas, period, parentheses and carriage returns. Keep in mind there can be be multiple separators together (e.g. many spaces, many commas together). The input is simple. You can assume a word is a string of letters (upper and lower case) and a number a string of digits (an integer without sign). Words and numbers will be separated by at least one non-letter or one non-digit symbol. Length: You can assume one word will be at most 30 characters long and a number will have at most 10 digits. Repeated strings: words and numbers can be repeated. However, you are not asked count distinct words or compute frequency per word, which require algorithms and data structures to be covered in the course. Therefore, you just simply need to count word or numver occurrences. For this homework we will not ask you to consider all potential inputs. Notice you are not asked to handle floating point numbers, which require decimal point and scientific notation.
input1.txt
------------------------------------------------------------ The cat is [there] 10 20 3.1416,,1000 another cat -------------------
5. Correct example
 #include \"ArgumentManager.h\"
 int main(int argc, char* argv[])
 {
 if (argc < 2) {
 std::cerr << \"Usage: count filename=input1.txt\ \";
 }
 ArgumentManager am(argc, argv);
 std::string filename = am.get(\"filename\");
 std::ifstream ifs(filename.c_str());
 std::string line;
 while (getline(ifs, line)){
 // replace symbols by space for line.
 // ...
 std::stringstream ss(line.c_str());
 std::string str;
 while (ss >> str) {
 if (is_number(str)) {
    // ...
 }
 else {
 // ...
 }
 }
 }
       
 return 0;
 }
Solution
/*
 Input file with the text is input.txt
 Some assumptions made:
 1. If the string between two special characters contain a mix of letters and numbers, the whole string is counted as 1 word.
 2. A new line character is also considered a special character
 
 Please revert if any of these assumptions are to be removed
 */
 #include <iostream>
 #include <fstream>
 #include <string>
 using namespace std;
 
 int main()
 {
     int count = 0;
     string line;
     ifstream myfile (\"input.txt\");
     if ( myfile.is_open() )
 {
     while ( getline (myfile,line) )
     {
       for ( unsigned int i = 0; i < line.length(); i++ )
             {
                 // if the character is from [a-z,A-Z,0-9]
                 if ((line[i] >= \'a\' && line[i] <= \'z\') || (line[i] >= \'A\' && line[i] <= \'Z\') || (line[i] >= \'0\' && line[i] <= \'9\'))
                 {
                     //To handle the case when there is one word in the whole file without any special character
                     if (i == 0)
                         count++;
                     continue;
                 }
                 // if the character is not from [a-z,A-Z,0-9]
                 else
                 {
                     // if the next character is from [a-z,A-Z,0-9] count it as a valid one
                     if ((line[i+1] >= \'a\' && line[i+1] <= \'z\') || (line[i+1] >= \'A\' && line[i+1] <= \'Z\') || (line[i+1] >= \'0\' && line[i+1] <= \'9\'))
                         count++;
 
                     // if the next character is not from [a-z,A-Z,0-9],ignore and continue
                     else
                         continue;
                 }
             }
     }
         cout << \"The total count is \" << count << endl;
     myfile.close();
 }
     else cout << \"Unable to open file\";
 }


