Skip to main content

Posts

Showing posts from September, 2025

Word Frequency Counter

Word Frequency Counter in Java Task Write a Java program to count the frequency of each word in a given sentence. The program should: 1. Take a sentence as input. 2. Split the sentence into individual words. 3. Count how many times each word appears. 4. Print each word along with its frequency. Example Input: the quick brown fox jumps over the lazy dog Output: the : 2 quick : 1 brown : 1 fox : 1 jumps : 1 over : 1 lazy : 1 dog : 1 --- Code import java.util.HashMap; import java.util.Map; public class WordFrequencyCounter {     public static void main(String[] args) {         // Input sentence         String sentence = "the quick brown fox jumps over the lazy dog";         // Split the sentence into words (using space as delimiter)         String[] words = sentence.split(" ");         // Create a HashMap to store words and their counts         Map<String, In...