6.20: Word Count (see attached screen print AND input file) Write a program that prompts the user to input the name of a text file and then outputs the number of words in the file. You can consider a "word" to be any text that is surrounded by whitespace (for example, a space, carriage return, newline) or borders the beginning or end of the file.Input Notes:There are two input sources in this program: the text file and standard input. The standard input consists of a single word, the name of the text file to be read.DO NOT USE A STRING VARIABLE. IF YOU USE A STRING, YOUR GRADE IS 0.00%. Output Notes (Prompts and Labels): The filename provided in standard input is prompted for with "Enter a file name: " and displayed so that the filename entered is on the same line as the prompt.The output of the program is a single line of the form: "The file contains N words." where N is the number of words determined by the program.

Question
Asked Nov 17, 2019
104 views

6.20: Word Count (see attached screen print AND input file)
 Write a program that prompts the user to input the name of a text file and then outputs the number of words in the file. You can consider a "word" to be any text that is surrounded by whitespace (for example, a space, carriage return, newline) or borders the beginning or end of the file.

Input Notes:There are two input sources in this program: the text file and standard input. The standard input consists of a single word, the name of the text file to be read.

DO NOT USE A STRING VARIABLE. IF YOU USE A STRING, YOUR GRADE IS 0.00%. 

Output Notes (Prompts and Labels): The filename provided in standard input is prompted for with "Enter a file name: " and displayed so that the filename entered is on the same line as the prompt.

The output of the program is a single line of the form: "The file contains N words." where N is the number of words determined by the program.

check_circle

Expert Answer

Step 1

Following is the c++ program:

Variables used:

f variable of type FILE is used to point to the file we want to read or write.

w of type int is used to count the number of words in the file.

name of type char is used to store the file name.

c of char type is used to store the fetched character from file.

Functions used:

Printf() used to print message on the console screen.

Scan() used to take input from console screen.

fopen() is used to open the file to perform read or write operation.

fgetc() is used to obtain single character from file at a time.  

Approach:

Main function is used to prompt the user to input the name of file. Open the file using fopen function in read mode. Fgetc is used to fetch character from file and store it in c variable, if character fetched is space then increment the word counter(w) by 1.

Step 2

Code:

#include<stdio.h>

 

#include<iostream>

 

int main() {

  FILE * f; //declaring file variable

  int w = 1; //variable to count number of words and is initialized to 1 because number

  //of words are 1 more than number of blank spaces.

  char name[50]; //variable to store name of file

  char c;

  printf("Enter a file name: ");

  scanf("%s", name); //read name of the file

 

  f = fopen(name, "r"); //open the file

  if (f == NULL)

    printf("file not found\n"); //print message if file is not found

  while ((c = fgetc(f)) != EOF) { //read character until EOF is reached

    if (c == ' ')

      w++; //increment variable w if whitespace is found

  }

  printf("\nThe file contain %d words", w);

  return 0;

}

Step 3

Content of Inpu...

main.cpp
file.txt
1 hello my name is code
help_outline

Image Transcriptionclose

main.cpp file.txt 1 hello my name is code

fullscreen

Want to see the full answer?

See Solution

Check out a sample Q&A here.

Want to see this answer and more?

Solutions are written by subject experts who are available 24/7. Questions are typically answered within 1 hour.*

See Solution
*Response times may vary by subject and question.
Tagged in

Engineering

Computer Science

Related Computer Science Q&A

Find answers to questions asked by student like you

Show more Q&A add
question_answer

Q: What trade-offs are involved in building highly distributed data environments?

A: Trade-offs that are involved in building highly distributed data environments: 1) Availability,  2) ...

question_answer

Q: Given the follwing business rules, create an ERD.  The ERD must include all primary keys, foreign ke...

A: The entities, primary keys and foreign keys and attributes of ERD are:

question_answer

Q: In this unit, we covered several tips for revising and editing documents. What do you believe is you...

A: Revising and editing featuresRevising and editing are the two tasks that are important for the writi...

question_answer

Q: Write a function so that the main() code below can be replaced by the simpler code that calls functi...

A: Program:#include &lt;iostream&gt;using namespace std;double MphAndMinutesToMiles(double milesPerHour...

question_answer

Q: Using DeMorgan's Law, write an expression for the complement of F ifF(x,y,z) = xz' (xy + xz)+ xy'(wz...

A: Expression:

question_answer

Q: What are the mantissa and exponent of 6.75 in 8-bit floating point binary system?

A: The given number is 6.75First, convert it in binary form.Binary form of 6 is 110Binary form of 0.75 ...

question_answer

Q: Create a class of type student .Members will include: name, gpa, rank (year) Make sure all data is p...

A: The name of the class is Student.All data is a private member of the class.All function is a public ...

question_answer

Q: Think of one of your Math homework. How can you use program to reveal the answer? Write a calculatio...

A: A program is to be created that uses at least 10 variables to calculate a problem. Let us consider a...

question_answer

Q: Python-Coding-WHAT COLOUR SQUARE  PART 1 - Positions on a chess board are identified by a letter and...

A: Following is the python program which takes as input from user the position of chessboard. Check usi...