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

   

C Program

#include <stdio.h>
#include <string.h>

int main() {
    char str[200], word[50];
    int count = 0;
    printf("Enter a sentence: ");
    fgets(str, sizeof(str), stdin);
    printf("Enter word to count: ");
    scanf("%s", word);

    char *token = strtok(str, " ,.-\n");
    while (token != NULL) {
        if (strcmp(token, word) == 0)
            count++;
        token = strtok(NULL, " ,.-\n");
    }

    printf("Occurrences of '%s': %d\n", word, count);
    return 0;
}

C Output

Input:
Enter a sentence: this is a test this is simple
Enter word to count: is

Output:
Occurrences of 'is': 2


C++ Program

#include <iostream>
#include <sstream>
using namespace std;

int main() {
    string str, word, temp;
    int count = 0;
    cout << "Enter a sentence: ";
    getline(cin, str);
    cout << "Enter word to count: ";
    cin >> word;

    stringstream ss(str);
    while (ss >> temp) {
        if (temp == word)
            count++;
    }
    cout << "Occurrences of '" << word << "': " << count << endl;
    return 0;
}

C++ Output

Input:
Enter a sentence: hello world hello hello
Enter word to count: hello

Output:
Occurrences of 'hello': 3


JAVA Program

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter a sentence: ");
        String str = sc.nextLine();
        System.out.print("Enter word to count: ");
        String word = sc.next();
        
        String[] words = str.split("\\s+");
        int count = 0;
        for (String w : words) {
            if (w.equals(word))
                count++;
        }
        System.out.println("Occurrences of '" + word + "': " + count);
    }
}

JAVA Output

Input:
Enter a sentence: java is fun java is powerful
Enter word to count: java

Output:
Occurrences of 'java': 2


Python Program

sentence = input("Enter a sentence: ")
word = input("Enter word to count: ")

words = sentence.split()
count = words.count(word)

print(f"Occurrences of '{word}': {count}")

Python Output

Input:
Enter a sentence: python is great and python is easy
Enter word to count: python

Output:
Occurrences of 'python': 2


In-Depth Explanation
Example
Let's say you have the sentence: "data is the new oil and data drives decisions". We want to know how many times the word "data" occurs. The logic breaks down the sentence into shorter pieces (words), and then it checks each piece against the word we are looking for. In this scenario, "data" is 2 times, so the response is 2.

Real-Life Analogy
Consider a class where the teacher utters various words while lecturing. If someone asks you, "How many times did the teacher use the word 'exam'?" you would replay the lecture in your mind, mark every instance of "exam" being used, and keep a count. The computer does precisely the same thing—it reads the text, word by word, and raises the counter each time it finds a match.

Why It Matters
Word count occurrence is a strong operation in text processing. Search engines such as Google base relevance ranking of a page for your query on word counts. Text analytics tools similarly employ word frequency in determining the sentiment in tweets, reviews, or feedback. Even plagiarism checkers or chatbots within themselves base their decisions on word occurrence verifications. 

Learning Insights
This course shows you how to tokenize (divide) a sentence into words and compare them in an organized manner. You learn string operations such as split, strtok, and playing with loops. Most importantly, you understand how raw data (an extended sentence) can be transformed into structured information (word counts). String problems are often underappreciated by novices, but they form the basis for high-end text mining, natural language processing (NLP), and even AI-based chatbots.

Interview Relevance
Interviewers enjoy posing this kind of question since it probes hands-on coding abilities. It demonstrates the way you work with strings, loops, if statements, and data structures such as arrays or lists. At times, they broaden the question: "Count all words and print their frequencies" or "Be case-insensitive." This also generates debate on hash maps, dictionaries, and efficiency while searching.

Real-World Use Cases
In Microsoft Word or Google Docs, the word counter operates on precisely this reasoning. When you compose an essay, the system tallies the occurrences of each word to present you with statistics. Analogously, in e-commerce reviews, businesses track how many times words such as "bad" or "excellent" appear in order to monitor customer satisfaction. In marketing, frequency analysis of product references on social media aids in monitoring brand popularity.

SEO Optimized Closing Paragraph
Word occurrence counting in a string is among the most useful problems in programming due to its similarity with actual applications such as word frequency counting, search engines, document processing, and sentiment analysis. Understanding this topic improves beginners' knowledge about handling strings, loops, and data processing and prepares them for coding interviews where text-based questions frequently appear. Practicing such word count utilities in C, C++, Java, and Python instills confidence in solving actual coding problems, and hence it is a must-learn part of programming for students and job seekers alike.