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:
Working with Tree Model Nodes in Jackson
Spring Boot - Runners
Hướng dẫn Java Design Pattern – Object Pool
Chương trình Java đầu tiên
Working with Kotlin and JPA
An Intro to Spring Cloud Zookeeper
Composition, Aggregation, and Association in Java
String Processing with Apache Commons Lang 3
REST Pagination in Spring
Chuyển đổi giữa các kiểu dữ liệu trong Java
REST Web service: Tạo ứng dụng Java RESTful Client với Jersey Client 2.x
wait() and notify() Methods in Java
Java Program to Find MST (Minimum Spanning Tree) using Prim’s Algorithm
Rest Web service: Filter và Interceptor với Jersey 2.x (P2)
Wiring in Spring: @Autowired, @Resource and @Inject
Java Program to Find a Good Feedback Vertex Set
Java Program to Check if a Matrix is Invertible
Java Program to Implement Direct Addressing Tables
Introduction to Using FreeMarker in Spring MVC
Java TreeMap vs HashMap
Versioning a REST API
HttpAsyncClient Tutorial
Spring – Injecting Collections
Spring Boot - OAuth2 with JWT
Understanding Memory Leaks in Java
Java Program to Check Whether an Undirected Graph Contains a Eulerian Cycle
Guide to @JsonFormat in Jackson
Đồng bộ hóa các luồng trong Java
Spring Boot - Admin Client
A Comparison Between Spring and Spring Boot
Java Program to Implement Cartesian Tree
Java Program to Implement Multi-Threaded Version of Binary Search Tree