Write a program that reads input as a stream of characters u

Write a program that reads input as a stream of characters until encountering EOF. Have the program report the number of words and the average number of letters per word. You can use the functions in ctype.h for this assignment. You can assume that whenever you encounter a whitespace character or punctuation that a word has ended. Use the attached file infile.txt as input when you submit your assignment.

Solution

code:

#include <iostream> // std::cin, std::cout
#include <fstream> // std::ifstream
using namespace std;
int main () {
char str[256];

  
ifstream is (\"example.txt\"); // open file

char c;
int len =0;
int avgsum = 0;
int noofwords =0;
if(is.is_open())
{
  
while (is.get(c))
{
   std::cout << c;
   if(c==\' \')
   {
      avgsum+=len;
      noofwords++;
      len=0;
   }
   len++;
}   
is.close();
}
else
cout<<\"unable to open file\"<<endl;
cout<<\"Total no of words are: \"<<noofwords<<endl;
cout<<\"average number of letters per word\"<<avgsum/noofwords<<endl;

is.close();
return 0;
}

Write a program that reads input as a stream of characters until encountering EOF. Have the program report the number of words and the average number of letters

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site