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 fileExplanationIn 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
SolutionPythoncount = 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 JAVAimport 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#
|