- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java program to count occurrences of a word in string
The number of times a word occurs in a string denotes its occurrence count. An example of this is given as follows −
String = An apple is red in colour. Word = red The word red occurs 1 time in the above string.
A program that demonstrates this is given as follows.
Example
public class Example { public static void main(String args[]) { String string = "Spring is beautiful but so is winter"; String word = "is"; String temp[] = string.split(" "); int count = 0; for (int i = 0; i < temp.length; i++) { if (word.equals(temp[i])) count++; } System.out.println("The string is: " + string); System.out.println("The word " + word + " occurs " + count + " times in the above string"); } }
Output
The string is: Spring is beautiful but so is winter The word is occurs 2 times in the above string
Now let us understand the above program.
The values of the string and word are provided. Then the string is split by spaces in temp. A for loop is used to find if the word is available in temp. Each time it happens, count is incremented by 1. The code snippet that demonstrates this is given as follows −
String string = "Spring is beautiful but so is winter"; String word = "is"; String temp[] = string.split(" "); int count = 0; for (int i = 0; i < temp.length; i++) { if (word.equals(temp[i])) count++; }
The string and the value of count i.e. number of times the word occurs in string is displayed. The code snippet that demonstrates this is given as follows −
System.out.println("The string is: " + string); System.out.println("The word " + word + " occurs " + count + " times in the above string");
- Related Articles
- C# program to count occurrences of a word in string
- Python program to count occurrences of a word in a string
- Write a python program to count occurrences of a word in string?
- How to replace all occurrences of a word in a string with another word in java?
- Java program to count the occurrences of each character
- How to Count Word Occurrences in a Text File using Shell Script?
- Count occurrences of a character in string in Python
- Java Program to replace all occurrences of a given character in a string
- Count occurrences of a substring recursively in Java
- Count occurrences of a character in a repeated string in C++
- Write a java program to reverse each word in string?
- Write a java program to tOGGLE each word in string?
- Python program to count occurrences of an element in a tuple
- Java Program to count vowels in a string
- Java Program to count letters in a String

Advertisements