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 – InputStream to Reader
Java Program to Print only Odd Numbered Levels of a Tree
Encode a String to UTF-8 in Java
Java Program to Perform Optimal Paranthesization Using Dynamic Programming
Java Program to Print the Kind of Rotation the AVL Tree is Undergoing
Java Program to Implement the Schonhage-Strassen Algorithm for Multiplication of Two Numbers
Injecting Prototype Beans into a Singleton Instance in Spring
Map Serialization and Deserialization with Jackson
Guide to @JsonFormat in Jackson
Java Program to Generate All Subsets of a Given Set in the Lexico Graphic Order
Using Custom Banners in Spring Boot
Java Program to Implement CountMinSketch
Java Program to Implement Sorted List
Rest Web service: Filter và Interceptor với Jersey 2.x (P1)
@Lookup Annotation in Spring
Ignore Null Fields with Jackson
Java Program to Implement Quick sort
Java Program to Implement the Hill Cypher
Java Program to Implement RoleUnresolvedList API
Compact Strings in Java 9
Request a Delivery / Read Receipt in Javamail
Java Program to Implement Quick Hull Algorithm to Find Convex Hull
Java Program to Implement Rolling Hash
Spring REST API + OAuth2 + Angular
Intro to Inversion of Control and Dependency Injection with Spring
Java Program to Implement Bresenham Line Algorithm
Guide to DelayQueue
Spring 5 Testing with @EnabledIf Annotation
Shuffling Collections In Java
Jackson – Unmarshall to Collection/Array
Java Program to Check Cycle in a Graph using Graph traversal
Java – Write to File