Javatpoint Logo
Javatpoint Logo

Program to find the number of words in the given text file

Explanation

In this program, we need to count the words present in given text file. This can be done by opening a file in read mode using file pointer. Read the file line by line. Split a line at a time and is stored in an array. Iterate through the array and count the word.

The content of data.txt file used in the program is shown below.

A computer program is a collection of instructions that performs specific task when executed by a computer.

Computer requires programs to function.

Computer program is usually written by a computer programmer in programming language.

A collection of computer programs, libraries, and related data are referred to as software.

Computer programs may be categorized along functional lines, such as application software and system software.

Algorithm

  1. Open a file in read mode using file pointer.
  2. Read a line from file.
  3. Split the line into words and store it in an array.
  4. Iterate through the array, increment count by 1 for each word.
  5. Repeat all these steps till all the lines from the files has been read.

Solution

Python

Output:

 Number of words present in given file: 63

C

Output:

Number of words present in given file: 63

JAVA

Output:

Number of words present in given file: 63

C#

Output:

Number of words present in given file: 63

PHP

Output:

Number of words present in given file: 63

Next Topic#




Please Share

facebook twitter google plus pinterest

Learn Latest Tutorials


B.Tech / MCA