Type-1:
Let's create a Java program that counts the occurrences of a specific word in a given text, along with a detailed explanation of how it works.
import java.util.Scanner;
public class WordCounter {
public static void main(String[] args) {
String text = "This is a sample text. The text contains a sample word.";
String wordToCount = "sample";
int occurrences = countOccurrences(text, wordToCount);
System.out.println("Occurrences of the word '" + wordToCount + "': " + occurrences);
}
public static int countOccurrences(String text, String word) {
// Convert both the text and the word to lowercase for case-insensitive matching
text = text.toLowerCase();
word = word.toLowerCase();
// Split the text into words using whitespace as the delimiter
String[] words = text.split("\\s+");
// Initialize a variable to store the count of occurrences
int count = 0;
// Iterate through each word in the text
for (String w : words) {
// Check if the word matches the given word
if (w.equals(word)) {
count++;
}
}
return count;
}
}
Explanation:
- We start with the main method, where we have the sample text and the wordToCount. The text variable holds the input text in which we want to count the occurrences of a specific word. The wordToCount variable represents the word for which we want to count occurrences. In this example, text is set to a sample text, and wordToCount is set to "sample."
- Next, we call the countOccurrences method, passing text and wordToCount as arguments, to get the count of occurrences of the specific word in the text.
- In the countOccurrences method, we first convert both the text and the word to lowercase using the toLowerCase() method. This step ensures case-insensitive matching, so occurrences of the word in any case (uppercase or lowercase) are counted correctly.
- We then split the text into individual words using the split() method with the regular expression "\s+" as the delimiter. This regular expression matches one or more whitespace characters, which effectively splits the text into words.
- We initialize a variable called count to store the count of occurrences of the specific word in the text. We set it to 0 initially.
- Next, we iterate through each word in the words array using a for-each loop.
- Inside the loop, we compare each word (w) with the word we want to count. If they match, we increment the count variable, indicating an occurrence of the word.
- After the loop, the count variable holds the total number of occurrences of the specific word in the text.
- Finally, we return the count from the countOccurrences method.
- The program then prints the result in the main method, displaying the occurrences of the specific word in the given text.
By implementing this program, we can efficiently count the occurrences of a specific word in a given text, regardless of its case and the presence of punctuation or special characters. The program considers only whole word matches, not partial matches within other words.
Type-2:
Another way to count the occurrences of a specific word in a given text is by using the indexOf() method in a loop. This approach involves finding the index of the word in the text iteratively until there are no more occurrences.
Let's create a Java program using this approach:
import java.util.Scanner;
public class WordCounter {
public static void main(String[] args) {
String text = "This is a sample text. The text contains a sample word.";
String wordToCount = "sample";
int occurrences = countOccurrences(text, wordToCount);
System.out.println("Occurrences of the word '" + wordToCount + "': " + occurrences);
}
public static int countOccurrences(String text, String word) {
// Convert both the text and the word to lowercase for case-insensitive matching
text = text.toLowerCase();
word = word.toLowerCase();
// Initialize a variable to store the count of occurrences
int count = 0;
// Find the first occurrence of the word in the text
int index = text.indexOf(word);
// Iterate through the text until there are no more occurrences
while (index != -1) {
count++;
// Move the index to the next position after the current occurrence
index = text.indexOf(word, index + 1);
}
return count;
}
}
Explanation:
- The code structure and the declaration of the main method remain the same as in the previous implementation.
- We call the countOccurrences method, passing text and wordToCount as arguments, to get the count of occurrences of the specific word in the text.
- In the countOccurrences method, we first convert both the text and the word to lowercase using the toLowerCase() method, ensuring case-insensitive matching, as before.
- We initialize a variable called count to store the count of occurrences of the specific word in the text. We set it to 0 initially.
- We find the index of the first occurrence of the word in the text using the indexOf() method.
- We enter a while loop that continues as long as the index is not -1. The indexOf() method returns -1 if there are no more occurrences of the word.
- Inside the loop, we increment the count variable, indicating an occurrence of the word.
- We then update the index to find the next occurrence of the word by calling indexOf() again, starting the search from the position after the current occurrence (index + 1).
- The loop continues until there are no more occurrences of the word.
- Finally, we return the count from the countOccurrences method.
This approach is efficient and works well for counting the occurrences of a specific word in a given text. It handles both uppercase and lowercase words, as well as words with punctuation or special characters. By using the indexOf() method in a loop, we can count all occurrences of the word without using regular expressions or complex string manipulations.
0 Comments