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:
“Stream has already been operated upon or closed” Exception in Java
Java Program to Encode a Message Using Playfair Cipher
Quick Guide to java.lang.System
Convert Character Array to String in Java
Spring WebFlux Filters
Prevent Cross-Site Scripting (XSS) in a Spring Application
Rest Web service: Filter và Interceptor với Jersey 2.x (P1)
Rest Web service: Filter và Interceptor với Jersey 2.x (P2)
Java Program to Implement Gauss Jordan Elimination
Java Program to Convert a Decimal Number to Binary Number using Stacks
Java Program to add two large numbers using Linked List
Tiêu chuẩn coding trong Java (Coding Standards)
Spring – Injecting Collections
Java Optional as Return Type
Handling URL Encoded Form Data in Spring REST
String Initialization in Java
New Features in Java 8
Java Program to Implement Binary Search Tree
Hướng dẫn tạo và sử dụng ThreadPool trong Java
Java Program to implement Dynamic Array
What is Thread-Safety and How to Achieve it?
Java Program to Perform Search in a BST
Java Program to Perform Sorting Using B-Tree
Using the Map.Entry Java Class
Java Program to Solve any Linear Equations
Converting Between an Array and a Set in Java
Convert Hex to ASCII in Java
Java Program to Compute Discrete Fourier Transform Using the Fast Fourier Transform Approach
Guava – Join and Split Collections
A Guide to ConcurrentMap
Spring Security Registration – Resend Verification Email
Java Program to Find the Mode in a Data Set