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:
Spring Boot - File Handling
Java Program to Find Transpose of a Graph Matrix
Hướng dẫn Java Design Pattern – Visitor
Java Program to Check whether Directed Graph is Connected using BFS
Java Byte Array to InputStream
Java Program to Describe the Representation of Graph using Incidence List
Versioning a REST API
Java Program to Implement RoleUnresolvedList API
Từ khóa static và final trong java
Java Program to Implement Gift Wrapping Algorithm in Two Dimensions
Java Optional as Return Type
Queue và PriorityQueue trong Java
Sử dụng Fork/Join Framework với ForkJoinPool trong Java
Semaphore trong Java
Java Program to Implement Naor-Reingold Pseudo Random Function
Lập trình đa luồng trong Java (Java Multi-threading)
Optional trong Java 8
Using Optional with Jackson
Java Program to Check Cycle in a Graph using Graph traversal
Debug a JavaMail Program
The XOR Operator in Java
Java Program to Implement Fermat Primality Test Algorithm
Implementing a Runnable vs Extending a Thread
Hướng dẫn sử dụng lớp Console trong java
Hướng dẫn sử dụng Java Annotation
Pagination and Sorting using Spring Data JPA
Giới thiệu Google Guice – Dependency injection (DI) framework
Java Program to Solve the 0-1 Knapsack Problem
Handling URL Encoded Form Data in Spring REST
Java Program to Implement Branch and Bound Method to Perform a Combinatorial Search
Java Program to Implement wheel Sieve to Generate Prime Numbers Between Given Range
Java Program to Construct an Expression Tree for an Postfix Expression