Question: Write a program that prints out the number of words in textfiles. We will define a word to be any sequence of non-whitespace characters. So

Write a program that prints out the number of words in textfiles. We will define a word to be any sequence of non-whitespace characters. So "hi&there...mom" would be considered a single word. Solve this problem by using a string variable into which you input each word as a string. Your program should ask the user for the name of the file to count words in, and then print the number of words in the file on the screen. It should continue to do this until the user types "quit" for the name of the file. You will make your life much easier if you do not use any variables of type char in this assignment. Use variables of type string instead. Turn in your source code and a sample output that has the user entering these 5 input files: file 1 | file 2 | file 3 | file 4 | file 5 Here is an example that does the same thing again, but allows the user to enter multiple files to be processed. #include #include #include using namespace std; int main() { char prevchar; char currchar; int count; string filename; ifstream infile; cout << "Enter the name of a file (or \"quit\"): "; cin >> filename; while (filename != "quit") { infile.open(filename); // infile.open(filename.c_str()); This syntax required in older (before 2011) C++ standards. if (!infile) { cout << "couldn't open file." << endl; } else { count = 0; infile.get(prevchar); infile.get(currchar); while (infile) { if (prevchar == '>' && currchar == '=') { count++; } prevchar = currchar; infile.get(currchar); } } infile.clear(); infile.close(); cout << "It occured " << count << " times." << endl; cout << "Enter the name of a file (or \"quit\"): "; cin >> filename; } } Here is an example that does the same thing again, but allows the user to enter multiple files to be processed. #include #include #include using namespace std; int main() { char prevchar; char currchar; int count; string filename; ifstream infile; cout << "Enter the name of a file (or \"quit\"): "; cin >> filename; while (filename != "quit") { infile.open(filename); // infile.open(filename.c_str()); This syntax required in older (before 2011) C++ standards. if (!infile) { cout << "couldn't open file." << endl; } else { count = 0; infile.get(prevchar); infile.get(currchar); while (infile) { if (prevchar == '>' && currchar == '=') { count++; } prevchar = currchar; infile.get(currchar); } } infile.clear(); infile.close(); cout << "It occured " << count << " times." << endl; cout << "Enter the name of a file (or \"quit\"): "; cin >> filename; } } This &%file should!!,... have exactly 7 words. the program requires to show the words count. Some file has space eg. This file must have several spaces at the end of the file. 19 words.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!