TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

Program to Find the Number of Words in the Given Text File

Program to Find the Number of Words in the Given Text File on fibonacci, factorial, prime, armstrong, swap, reverse, search, sort, stack, queue, array, linkedlist, tree, graph, pattern, string etc.

<< Back to PROGRAM

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.

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.

Solution

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

Next Topic#




Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf