Write a program that prints out the number of words in textfiles. We will define
ID: 3840665 • Letter: W
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 "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; } }
Explanation / Answer
SOLUTION
Below is the code written in c++ to count words in a file. Copy and paste code into a code editor for better understanding. Compile it and run it. If you have any doubts kindly comment down. If you find this helpful upvote it. Happy Coding.
#include <iostream>
#include <fstream>
using namespace std;
int main(){
string file_name; /* variable that stores filename */
cout << "Enter the name of a file (or "quit"): ";
cin>>file_name; /* take user input of file name */
while(file_name.compare("quit")!=0){
ifstream infile; /* object for file */
infile.open(file_name.c_str()); /* open file*/
if(infile.fail()){ /* if failed to open file */
cout << "couldn't open file." << endl;
}
else{
int word_count=0; /* initialize word count to 0*/
string word; /* string to store word */
while(!infile.eof()){ /* while end of file is not reached*/
infile>>word;
word_count+=1;
}
cout<<"Number of words: "<<word_count<<endl; /* print number of words*/
}
cout << "Enter the name of a file (or "quit"): ";
cin>>file_name; /* take user input of file name */
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.