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 Circular Doubly Linked List
Java Program to Perform Partition of an Integer in All Possible Ways
Java – Reader to InputStream
Toán tử instanceof trong java
Logging in Spring Boot
Spring Boot - Service Components
Giới thiệu Json Web Token (JWT)
Java Program to Use rand and srand Functions
Java 9 Stream API Improvements
Spring Boot - Admin Server
Java Program to Find the Longest Subsequence Common to All Sequences in a Set of Sequences
Overview of the java.util.concurrent
Redirect to Different Pages after Login with Spring Security
Summing Numbers with Java Streams
Java Program to Search Number Using Divide and Conquer with the Aid of Fibonacci Numbers
Giới thiệu luồng vào ra (I/O) trong Java
Java Program to Solve TSP Using Minimum Spanning Trees
Java Program to Implement Gift Wrapping Algorithm in Two Dimensions
Adding a Newline Character to a String in Java
Hướng dẫn Java Design Pattern – Iterator
Extract links from an HTML page
Spring WebFlux Filters
Java Program to Implement ConcurrentLinkedQueue API
Java Program to Implement RoleList API
Giới thiệu thư viện Apache Commons Chain
Spring Boot - Hystrix
Java Program to Find Shortest Path Between All Vertices Using Floyd-Warshall’s Algorithm
Implementing a Binary Tree in Java
So sánh HashSet, LinkedHashSet và TreeSet trong Java
Spring REST API + OAuth2 + Angular
Java Program to Represent Graph Using Incidence Matrix
A Comparison Between Spring and Spring Boot