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 Program to Implement Sorted Circular Doubly Linked List
Spring @RequestParam Annotation
Instance Profile Credentials using Spring Cloud
Java Program to Solve any Linear Equation in One Variable
Deploy a Spring Boot App to Azure
Checking for Empty or Blank Strings in Java
Hướng dẫn Java Design Pattern – Strategy
Java Program to Implement Graph Structured Stack
String Processing with Apache Commons Lang 3
Spring MVC Custom Validation
Java Program to Implement Ternary Search Algorithm
Hướng dẫn Java Design Pattern – Interpreter
Spring Cloud – Bootstrapping
Wiring in Spring: @Autowired, @Resource and @Inject
Java Program to Perform Searching Based on Locality of Reference
Java Program to Implement Euler Circuit Problem
Từ khóa this và super trong Java
Server-Sent Events in Spring
Java Program to Implement Trie
Java Program to Find Nearest Neighbor for Dynamic Data Set
Java Program to Perform Arithmetic Operations on Numbers of Size
Java Program to Implement LinkedHashMap API
Extract network card address
Biểu thức Lambda trong Java 8 – Lambda Expressions
Creating Docker Images with Spring Boot
Java Program to Implement HashTable API
So sánh HashMap và HashSet trong Java
New Features in Java 10
Spring Boot - Runners
Java Program to Implement CopyOnWriteArraySet API
Java – Rename or Move a File
What is a POJO Class?