Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Big Data Systems A 360-degree Approach

Authors: Jawwad ShamsiMuhammad Khojaye

1st Edition

0429531575, 9780429531576

More Books

Students also viewed these Databases questions