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:
DistinctBy in the Java Stream API
Collect a Java Stream to an Immutable Collection
Spring – Injecting Collections
Generate Spring Boot REST Client with Swagger
Testing in Spring Boot
Wiring in Spring: @Autowired, @Resource and @Inject
LinkedHashSet trong Java hoạt động như thế nào?
Reading an HTTP Response Body as a String in Java
Java Program to Implement Hash Trie
Java Program to Implement TreeMap API
Java Program to Represent Graph Using Adjacency List
JUnit 5 for Kotlin Developers
Sending Emails with Java
Java Program to Perform Cryptography Using Transposition Technique
SOAP Web service: Upload và Download file sử dụng MTOM trong JAX-WS
Quick Guide on Loading Initial Data with Spring Boot
Java Program to Implement Range Tree
Spring Autowiring of Generic Types
Java Program to Find Minimum Number of Edges to Cut to make the Graph Disconnected
Java Program to Check whether Graph is a Bipartite using 2 Color Algorithm
Spring WebClient vs. RestTemplate
Prevent Cross-Site Scripting (XSS) in a Spring Application
Runnable vs. Callable in Java
Java Program to Perform the Shaker Sort
Jackson Unmarshalling JSON with Unknown Properties
Java Program to Generate Randomized Sequence of Given Range of Numbers
Java Program to Implement Shunting Yard Algorithm
Introduction to the Java NIO2 File API
Hướng dẫn Java Design Pattern – Builder
Collection trong java
Read an Outlook MSG file
Period and Duration in Java