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:
Predicate trong Java 8
Migrating from JUnit 4 to JUnit 5
Java Program to Implement Sparse Array
Java Program to find the maximum subarray sum using Binary Search approach
Java Program to Implement Interpolation Search Algorithm
Query Entities by Dates and Times with Spring Data JPA
Merging Two Maps with Java 8
Java Program to Repeatedly Search the Same Text (such as Bible by building a Data Structure)
Introduction to Spring Cloud Netflix – Eureka
Annotation trong Java 8
Một số nguyên tắc, định luật trong lập trình
Overview of Spring Boot Dev Tools
Guide to the Synchronized Keyword in Java
Consuming RESTful Web Services
The Modulo Operator in Java
Convert String to Byte Array and Reverse in Java
Java Program to Implement Variable length array
Introduction to the Functional Web Framework in Spring 5
Logging in Spring Boot
Java Program to Check the Connectivity of Graph Using DFS
Java Program to Implement the Vigenere Cypher
Java Program to Construct an Expression Tree for an Infix Expression
Java Program to Implement Ternary Search Tree
Introduction to the Java NIO Selector
Spring Security Custom AuthenticationFailureHandler
Java Program to Evaluate an Expression using Stacks
Java Program to Implement Ternary Heap
Java Program to find the maximum subarray sum O(n^2) time(naive method)
Java Program to Give an Implementation of the Traditional Chinese Postman Problem
Java Program to Find MST (Minimum Spanning Tree) using Prim’s Algorithm
Handle EML file with JavaMail
Remove All Occurrences of a Specific Value from a List