In: Computer Science
Write a program that reads a text file and reports the total count of words of each length. A word is defined as any contiguous set of alphanumeric characters, including symbols. For example, in the current sentence there are 10 words. The filename should be given at the command line as an argument. The file should be read one word at a time. A count should be kept for how many words have a given length. For example, the word “frog” is 4 bytes in length; the word “turtle” is 6 bytes in length. The program should report the total word counts of all lengths between 3 and 15 bytes. Words with lengths outside that range should not be counted
Code
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
int main(int argc, char *argv[])
{
string fileName=argv[1];
string word;
int count=0;
ifstream inFile;
inFile.open(fileName);
if(!inFile)
{
cout<<"Error! Unable to opne the file. Exting...."<<endl;
return 0;
}
while(inFile>>word)
{
if(word.length()>3 && word.length()<15)
count++;
}
cout<<"The total word counts of all lengths between 3 and 15 bytes are: "<<count<<endl;
return 1;
}
input.txt file
output
If you have any query regarding the code please ask me in the comment i am here for help you. Please do not direct thumbs down just ask if you have any query. And if you like my work then please appreciates with up vote. Thank You.