Java Program to Count the Number of Occurrences of a Substring in a String

Ramesh Fadatare
3 min readDec 13, 2024

--

Introduction

Counting the number of occurrences of a substring within a string is a common task in text processing. This exercise helps you understand how to work with strings and use methods in Java to search for substrings. This guide will walk you through writing a Java program that counts the number of times a given substring appears in a string.

Learn everything about Java: https://www.javaguides.net/

Problem Statement

Create a Java program that:

  • Prompts the user to enter a string.
  • Prompts the user to enter a substring to search for.
  • Counts the number of occurrences of the substring in the string.
  • Displays the count of occurrences.

Example:

  • Input String: "Java is a programming language. Java is also an island. Java is popular."
  • Substring to search: "Java"
  • Output: The substring 'Java' occurs 3 times.

Solution Steps

  1. Read the String: Use the Scanner class to take the main string as input from the user.
  2. Read the Substring: Use the Scanner class to take the substring to search for.
  3. Count Occurrences: Use a loop and the indexOf() method to count how many times the substring appears in the string.
  4. Display the Count: Print the total number of occurrences of the substring.
  5. Close Resources: Close the Scanner class object automatically using the try-resource statement.

Java Program

// Java Program to Count the Number of Occurrences of a Substring in a String

import java.util.Scanner;

public class SubstringOccurrenceCounter {
public static void main(String[] args) {
// Step 1: Read the string and the substring from the user
try (Scanner scanner = new Scanner(System.in)) {
System.out.print("Enter the main string: ");
String mainString = scanner.nextLine();

System.out.print("Enter the substring to search for: ");
String substring = scanner.nextLine();

// Step 2: Count occurrences of the substring
int count = 0;
int fromIndex = 0;

while ((fromIndex = mainString.indexOf(substring, fromIndex)) != -1) {
count++;
fromIndex += substring.length();
}

// Step 3: Display the count of occurrences
System.out.println("The substring '" + substring + "' occurs " + count + " times.");
}
}
}

Explanation

Step 1: Read the String and Substring

  • The Scanner class is used to read the main string and the substring input from the user. The nextLine() method captures the entire line as a string.

Step 2: Count Occurrences of the Substring

  • The indexOf() method is used to find the first occurrence of the substring in the main string, starting from a given index (fromIndex). If the substring is found, indexOf() returns the index of the first character of the substring. If the substring is not found, it returns -1.
  • The loop continues to search for the substring by updating the fromIndex to the index immediately after the last found occurrence. This ensures that overlapping occurrences are also counted.

Step 3: Display the Count of Occurrences

  • The program prints the total number of occurrences of the substring in the main string using System.out.println().

Output

Enter the main string: Java is a programming language. Java is also an island. Java is popular.
Enter the substring to search for: Java
The substring 'Java' occurs 3 times.

Conclusion

This Java program demonstrates how to count and display the number of occurrences of a substring within a user-input string. It covers essential concepts such as string manipulation, using the indexOf() method, and handling loops, making it a valuable exercise for beginners learning Java programming.

Learn everything about Java: https://www.javaguides.net/

--

--

No responses yet