ICSE Computer Science Java Programs | IT Developer
IT Developer

Java Programs - Solved 2007 ICSE Computer Science Paper



Share with a Friend

Solved 2007 ICSE Computer Science Paper

Class 10 - ICSE Computer Science Solved Papers

Count of a word in String Program - ICSE 2007 Computer Science

Write a program to enter a sentence from the keyboard and count the number of times a particular word occurs in it. Display the frequency of the search word.
Example:
INPUT:
Enter the sentence: the quick brown fox jumps over the lazy dog.
Enter a word to be searched: the
OUTPUT:
Searched word occurs 2 times.

import java.io.*; class Frequency{ public static void main(String args[])throws IOException{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); System.out.print("Enter the sentence: "); String s = br.readLine(); System.out.print("Enter the word to be searched: "); String key = br.readLine(); String word = ""; int count = 0; for(int i = 0; i < s.length(); i++){ char ch = s.charAt(i); if(Character.isLetterOrDigit(ch)) word += ch; else{ if(word.equalsIgnoreCase(key)) count++; word = ""; } } System.out.println("Searched word occurs " + count + " times."); } }

Output

 
 OUTPUT  : 
Enter the sentence: the quick brown fox jumps over the lazy dog.
Enter the word to be searched: the
Searched word occurs 2 times.