Write a program that reads a text file and reports the totalcount of words of ea
ID: 3614892 • Letter: W
Question
Write a program that reads a text file and reports the totalcount of words of each length.A word is defined as any contiguous set of alphanumeric characters,including symbols. For example, in the cuurrent sentence there are 12words. The file should be given at the command line as anargument. (1)The file should be read one word at a time. (2) A count should be kept for how many words have agiven length. For example, the word "frog" is 4 bytes in length; the word"turtle" is 6 bytes in length. (3)The program should report total word counts of all lengthsbetween 3 and 15 bytes. Words with lengths outside that rangeshould not be counted. For example, in the cuurrent sentence there are 12words. The file should be given at the command line as anargument. (1)The file should be read one word at a time. (2) A count should be kept for how many words have agiven length. For example, the word "frog" is 4 bytes in length; the word"turtle" is 6 bytes in length. (3)The program should report total word counts of all lengthsbetween 3 and 15 bytes. Words with lengths outside that rangeshould not be counted.
Explanation / Answer
#include <iostream> #include <fstream> #include <string> using namespace std;int main () { string line; char str[100]; int len,count[20],i; memset(count,0,sizeof(count)); ifstream myfile ("input.txt"); if (myfile.is_open()) { while(!myfile.eof()) { myfile>>str; cout<<str<<endl;len=strlen(str); if(len>=3 && len<=15) count[len]++; } myfile.close(); for(i=3;i<=15;i++) if(count[i]!=0) cout<<"Count of length"<<i<<" is :"<<count[i]<<endl; }
else { cout << "Unable to open fileinput.txt"; } system("pause"); return 0; }
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.