Answer to Question #210878 in C++ for Abende

Question #210878

Complete Program.

int  word_count(ifstream &in ); // return the number of words in text 1

int character_count(ifstream &in); // return the number of characters

 #include "stdafx.h"

#include <iostream>

# include <string>

# include<fstream>

using namespace std;

int main()

{     ifstream in; in.open(“text1.txt”);

                   cout<< “number of words”<< word_count(in )<<endl;

                   cout<< “number of characters”<<character_count(in)<<endl;

                    in.close( );

            }


1
Expert's answer
2021-06-27T14:11:36-0400
#include <iostream>
#include <string>
#include <fstream>

using namespace std;

int word_count(ifstream & in) 
{
  int word = 1; 
  char ch;
  in.seekg(0, ios::beg); 
  while (in) {
    in.get(ch);
    if(ch == ' ' || ch == '\n')
      word++;
  }
  return word;
}

int character_count(ifstream & in) 
{
  char ch;
  int i, c = 0, sp = 0;
  while (in) {
    in.get(ch);
    i = ch;
    if((i > 63 && i < 91) || (i > 96 && i < 123))
      c++;
    else if(ch== ' ')
      sp++;
  }
  return sp;
}

int main()
{
  ifstream in;
  in.open("text1.txt");
  
  cout << "number of words" <<  word_count(in) << endl;
  cout << "number of characters" << character_count(in) << endl;

  return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS