Java Program to Implement the String Search Algorithm for Short Text Sizes

This is a java program to search string using simple algorithm BoyerMoore.

Here is the source code of the Java Program to Implement the String Search Algorithm for Short Text Sizes. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

package com.maixuanviet.setandstring;
 
import java.util.Scanner;
 
class BoyerMoore
{
    private final int BASE;
    private int[]     occurrence;
    private String    pattern;
 
    public BoyerMoore(String pattern)
    {
        this.BASE = 256;
        this.pattern = pattern;
        occurrence = new int[BASE];
        for (int c = 0; c < BASE; c++)
            occurrence = -1;
        for (int j = 0; j < pattern.length(); j++)
            occurrence[pattern.charAt(j)] = j;
    }
 
    public int search(String text)
    {
        int n = text.length();
        int m = pattern.length();
        int skip;
        for (int i = 0; i <= n - m; i += skip)
        {
            skip = 0;
            for (int j = m - 1; j >= 0; j--)
            {
                if (pattern.charAt(j) != text.charAt(i + j))
                {
                    skip = Math.max(1, j - occurrence[text.charAt(i + j)]);
                    break;
                }
            }
            if (skip == 0)
                return i;
        }
        return n;
    }
}
 
public class StringSearch
{
    public static void main(String[] args)
    {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter the main string: ");
        String text = sc.nextLine();
        System.out.print("Enter the string to be searched: ");
        String pattern = sc.nextLine();
        BoyerMoore bm = new BoyerMoore(pattern);
        int first_occur_position = bm.search(text);
        System.out.println("The text '" + pattern
                + "' is first found after the " + first_occur_position
                + " position.");
        sc.close();
    }
}

Output:

$ javac StringSearch.java
$ java StringSearch
 
Enter the main string: I will soon be working in Redmond USA with Mircosoft.
Enter the string to be searched: soon
The text 'soon' is first found after the 7 position.

Related posts:

Returning Custom Status Codes from Spring Controllers
Hướng dẫn sử dụng Java String, StringBuffer và StringBuilder
Fixing 401s with CORS Preflights and Spring Security
How to use the Spring FactoryBean?
Java Program to Implement Sorted Doubly Linked List
Thao tác với tập tin và thư mục trong Java
Comparing Arrays in Java
Control Structures in Java
Loại bỏ các phần tử trùng trong một ArrayList như thế nào trong Java 8?
Java Program to Use Above Below Primitive to Test Whether Two Lines Intersect
Java Program to Find Number of Articulation points in a Graph
Java Program to Implement Min Heap
Java Program to Solve Travelling Salesman Problem for Unweighted Graph
Introduction to Apache Commons Text
Java Program to Perform Addition Operation Using Bitwise Operators
Java Program to Represent Graph Using Incidence List
Adding a Newline Character to a String in Java
Introduction to Spring Cloud Rest Client with Netflix Ribbon
Java Program to Implement Hash Trie
Stack Memory and Heap Space in Java
The Registration Process With Spring Security
Java Program to Check whether Undirected Graph is Connected using DFS
New in Spring Security OAuth2 – Verify Claims
Luồng Daemon (Daemon Thread) trong Java
Immutable Objects in Java
Java Program to Compute the Volume of a Tetrahedron Using Determinants
Using Spring @ResponseStatus to Set HTTP Status Code
Java Program to Implement Hash Tables
Spring Cloud Connectors and Heroku
Spring 5 Functional Bean Registration
Spring Webflux with Kotlin
Java Program to Implement the Binary Counting Method to Generate Subsets of a Set