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 -------------------
Solution
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
// declare variables
int numUpperCase = 0;
int numVowel = 0;
int numChars = 0;
char character = \' \';
cout << \"Enter a sentence: \";
// Get sentence from user utilizing while loop
while(cin >> character && character != \'.\')
{
// Checks to see if inputted data is UPPERCASE
if ((character >= \'A\')&&(character <= \'Z\'))
{
++numUpperCase; // Increments Total number of uppercase by one
}
// Checks to see if inputted data is a vowel
if ((character == \'a\')||(character == \'A\')||(character == \'e\')||
(character == \'E\')||(character == \'i\')||(character == \'I\')||
(character == \'o\')||(character == \'O\')||(character == \'u\')||
(character == \'U\')||(character == \'y\')||(character == \'Y\'))
{
++numVowel; // Increments Total number of vowels by one
}
++numChars; // Increments Total number of chars by one
} // end loop
// display data using setw and setfill
cout << setfill(\'.\'); // This will fill any excess white space with period within the program
cout <<left<<setw(36)<<\"\ \\tTotal number of upper case letters:\"<<right<<setw(10)
<<numUpperCase<<endl;
cout <<left<<setw(36)<<\"\\tTotal number of vowels:\"<<right<<setw(10)
<<numVowel<<endl;
cout <<left<<setw(36)<<\"\\tTotal number of characters:\"<<right<<setw(10)
<<numChars<<endl;
system(\"pause\");
return 0;
}

