String Handling — Question 23
Back to all questionsQuestion 10
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.
Sample Input:
Enter a sentence: The quick brown fox jumps over the lazy dog.
Enter a word to search: the
Sample Output:
Search word occurs 2 times
import java.util.Scanner;
public class KboatWordFrequency
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter a sentence: ");
String str = in.nextLine();
System.out.print("Enter a word to search: ");
String ipWord = in.nextLine();
str += " ";
String word = "";
int count = 0;
int len = str.length();
for (int i = 0; i < len; i++) {
if (str.charAt(i) == ' ') {
if (word.equalsIgnoreCase(ipWord))
count++ ;
word = "";
}
else {
word += str.charAt(i);
}
}
if (count > 0) {
System.out.println("Search word occurs " + count + " times.");
}
else {
System.out.println("Search word is not present in sentence.");
}
}
}Output

Key Concepts Covered
This question tests your understanding of the following concepts from the chapter String Handling: Question, Program, Enter, Sentence, Keyboard, Count. These are fundamental topics in Computer Applications that students are expected to master as part of the ICSE Class 10 curriculum.
A thorough understanding of these concepts will help you answer similar questions confidently in your ICSE examinations. These topics are frequently tested in both objective and subjective sections of Computer Applications papers. We recommend revising the relevant section of your textbook alongside practising these solved examples to build a strong foundation.
How to Approach This Question
Read the question carefully and identify what is being asked. Break down complex questions into smaller parts. Use the terminology and concepts discussed in this chapter. Structure your answer logically — begin with a definition or key statement, then provide supporting details. Review your answer to ensure it addresses all parts of the question completely.
Key Points to Remember
- Write programs with proper indentation and comments.
- Trace through your code with sample inputs to verify correctness.
- Explain the logic behind each step of your solution.
- Familiarise yourself with common library functions and methods.
Practice more questions from String Handling — Computer Applications, Class 10 ICSE