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 Perform Sorting Using B-Tree
A Custom Data Binder in Spring MVC
Guide to BufferedReader
Testing in Spring Boot
Java – Rename or Move a File
Java Program to Implement Nth Root Algorithm
Toán tử instanceof trong java
Java Program to Print only Odd Numbered Levels of a Tree
Java Program to Implement TreeSet API
Java Program to Perform Preorder Non-Recursive Traversal of a Given Binary Tree
Java Program to Perform Inorder Non-Recursive Traversal of a Given Binary Tree
Một số nguyên tắc, định luật trong lập trình
Java Program to Find ith Largest Number from a Given List Using Order-Statistic Algorithm
Java Program to Implement Borwein Algorithm
Apache Commons Collections BidiMap
Generating Random Numbers in a Range in Java
Autoboxing và Unboxing trong Java
HashSet trong java
Login For a Spring Web App – Error Handling and Localization
Java 14 Record Keyword
Java Program to Perform Insertion in a 2 Dimension K-D Tree
Java Program to Implement LinkedHashMap API
A Guide to JUnit 5 Extensions
Spring Boot - Hystrix
Introduction to the Java NIO Selector
Disable Spring Data Auto Configuration
Java Program to Implement Suffix Array
Java Program to Perform Matrix Multiplication
Java Program to Implement Graham Scan Algorithm to Find the Convex Hull
Convert a Map to an Array, List or Set in Java
Java Program to Implement Queue
Java Program to Implement Hamiltonian Cycle Algorithm