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:
Java 8 – Powerful Comparison with Lambdas
Java toString() Method
Spring Boot - Eureka Server
Java Program to Implement Shunting Yard Algorithm
Guide to the Java ArrayList
Generating Random Numbers in a Range in Java
Read an Outlook MSG file
Java Program to Find MST (Minimum Spanning Tree) using Prim’s Algorithm
Java Program to Implement Jarvis Algorithm
Java Program to Implement Maximum Length Chain of Pairs
Java Program to Implement Segment Tree
Java Program to Check Whether Graph is DAG
Java Map With Case-Insensitive Keys
Wrapper Classes in Java
Java Program to Implement Karatsuba Multiplication Algorithm
HttpAsyncClient Tutorial
Registration – Password Strength and Rules
LIKE Queries in Spring JPA Repositories
Java Program to Implement SynchronosQueue API
How to Add a Single Element to a Stream
Show Hibernate/JPA SQL Statements from Spring Boot
Java Program to Create a Balanced Binary Tree of the Incoming Data
Java Program to Check Whether it is Weakly Connected or Strongly Connected for a Directed Graph
Java – InputStream to Reader
Java Streams vs Vavr Streams
Hướng dẫn Java Design Pattern – Builder
The Order of Tests in JUnit
Introduction to Spring Data MongoDB
Java Program to Perform Polygon Containment Test
Java – Generate Random String
Creating a Custom Starter with Spring Boot
Java Program to Perform LU Decomposition of any Matrix