Count Occurrences of a Word in a File in C, C++, Java & Python – Code with Explanation & Examples in Short and Simple

   

C Program

#include <stdio.h>
#include <string.h>
int main() {
    FILE *f = fopen("file.txt", "r");
    char w[20], temp[20]; int c = 0;
    scanf("%s", w);
    while (fscanf(f, "%s", temp) != EOF)
        if (!strcmp(w, temp)) c++;
    printf("Count: %d", c);
}

C Output

Input (file.txt):
hello world hello code

Input (console):
hello

Output:
Count: 2



C++ Program

#include <fstream>
#include <iostream>
using namespace std;
int main() {
    ifstream f("data.txt"); string w, s; int c = 0;
    cin >> w;
    while (f >> s) if (s == w) c++;
    cout << "Count: " << c;
}

C++ Output

Input (data.txt):
apple orange banana apple mango

Input (console):
apple

Output:
Count: 2



JAVA Program

import java.util.*; import java.io.*;
public class Main {
    public static void main(String[] a) throws Exception {
        Scanner s = new Scanner(new File("log.txt"));
        Scanner in = new Scanner(System.in);
        String w = in.next(), temp; int c = 0;
        while (s.hasNext()) if ((temp = s.next()).equals(w)) c++;
        System.out.println("Count: " + c);
    }
}

JAVA Output

Input (log.txt):
run run jog sprint run walk

Input (console):
run

Output:
Count: 3



Python Program

w = input(); print("Count:", open("words.txt").read().split().count(w))

Python Output

Input (words.txt):
red blue red green red

Input (console):
red

Output:
Count: 3



In-Depth Explanation
Example
Suppose a file has the following text: red blue red green red, and the user types the word red. The program breaks the file into words and counts the occurrences of red. The output in this case will be 3.

Real-Life Analogy
Consider looking your name up in a long PDF with Ctrl+F. It displays how many times your name is mentioned. That's just what this logic emulates: word counting on autopilot for big text files.

Why It Matters
This kind of logic is at the core of:

Text search engines

Word frequency analysis

Log analysis in system files

File auditing and reporting

Regardless of whether you're creating a search or examining text content, this logic lies at the center.

Learning Insights
This course teaches:

File I/O (reading files)

String comparison (==, .equals(), .count())

Looping through text word by word

Handling case-sensitive searches (can be extended to case-insensitive)

You also learn how various languages read files. C uses fscanf, C++ uses streams, Java uses Scanner, and Python uses direct file reads with built-in string operations.

Interview & Real-World Relevance
This problem comes up in interviews for positions that require:

File handling

Text parsing

String logic

Data analysis basics

It also makes up the foundation logic for utilities such as:

Text analyzers

Log parsers

Chat history counters

Word cloud generators

Word occurrences counting in a file is a traditional file handling and string processing task. Whether you are developing a log analyzer, a word counter, or a specific search engine, this code provides a starting point for real data analysis. These simple, language-specific implementations in C, C++, Java, and Python give you a good base for learning how to deal with files and draw useful conclusions from raw text.