Q:

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

belongs to collection: String Programs

0

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.

data.txt  

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.

Input:

file = open("data.txt", "r")  

data.txt file content:

The term "computer" is derived from Latin word "computare" which means to calculate. Computer is a programmable electronic device. Computer accepts raw data as input and processes it with set of instructions to produce result as output. The history of computer begins with the birth of abacus which is believed to be the first computer.

Output:

Number of words present in given file: 117

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Python

count = 0;  
   
#Opens a file in read mode  
file = open("data.txt", "r")  
      
#Gets each line till end of file is reached  
for line in file:  
    #Splits each line into words  
    words = line.split(" ");  
    #Counts each word  
    count = count + len(words);  
   
print("Number of words present in given file: " + str(count));  
file.close();  

 

Output:

 Number of words present in given file: 63

 

C

#include <stdio.h>  
#include <stdlib.h>  
   
int main()  
{   char ch;  
    FILE *file;  
    int count = 0;  
      
    //Opens a file in read mode  
    file = fopen("data.txt","r");  
      
    //Gets each character till end of file is reached  
    while((ch = fgetc(file)) != EOF){  
        //Counts each word  
        if(ch ==' ' || ch == '\n')  
            count++;  
    }  
      
    printf("Number of words present in given file: %d", count);  
    fclose(file);  
      
    return 0;  
}  

 

Output:

Number of words present in given file: 63

 

JAVA

import java.io.BufferedReader;  
import java.io.FileReader;  
   
public class CountWordFile   
{  
    public static void main(String[] args) throws Exception {  
        String line;  
        int count = 0;  
          
        //Opens a file in read mode  
        FileReader file = new FileReader("data.txt");  
        BufferedReader br = new BufferedReader(file);  
              
        //Gets each line till end of file is reached  
        while((line = br.readLine()) != null) {  
            //Splits each line into words  
            String words[] = line.split(" ");  
            //Counts each word  
            count = count + words.length;  
        }  
          
        System.out.println("Number of words present in given file: " + count);  
        br.close();  
    }  
}  

 

Output:

Number of words present in given file: 63

 

C#

using System;  
                      
public class CountWordFile  
{      
    public static void Main()  
    {  
        String line;  
        int count = 0;  
          
        //Opens a file in read mode  
        System.IO.StreamReader file = new System.IO.StreamReader(@"data.txt");    
              
        //Gets each line till end of file is reached  
        while((line = file.ReadLine()) != null){  
            //Splits each line into words  
            String[] words= line.Split(' ');  
            //Counts each word  
            count = count + words.Length;  
        }  
          
        Console.WriteLine("Number of words present in given file: " + count);  
        file.Close();  
    }  
}  

 

Output:

Number of words present in given file: 63

 

PHP

<!DOCTYPE html>  
<html>  
<body>  
<?php  
$count = 0;  
   
//Opens a file in read mode  
$file = fopen("data.txt", "r");  
      
//Gets each line till end of file is reached  
while (($line = fgets($file)) !== false) {  
    //Splits each line into words  
    $words = explode(" ", $line);  
    //Counts each word  
    $count = $count + count($words);  
}  
   
print("Number of words present in given file: " . $count);  
fclose($file);  
?>  
</body>  
</html>  

 

Output:

Number of words present in given file: 63

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions


Program to print smallest and biggest possible pal... >>
<< Program to find the most repeated word in a text f...