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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 | 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:
1 2 3 4 5 6 | $ 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:
Assertions in JUnit 4 and JUnit 5
Converting Between a List and a Set in Java
Java Program to Find Shortest Path Between All Vertices Using Floyd-Warshall’s Algorithm
Number Formatting in Java
Guide to Apache Commons CircularFifoQueue
Connect through a Proxy
Guide to Dynamic Tests in Junit 5
Java – Random Long, Float, Integer and Double
Java Program to Solve Travelling Salesman Problem for Unweighted Graph
Java Program to Compute Discrete Fourier Transform Using Naive Approach
Spring Boot Gradle Plugin
Removing all Nulls from a List in Java
Java Program to Generate Random Partition out of a Given Set of Numbers or Characters
Jackson JSON Views
Using Spring ResponseEntity to Manipulate the HTTP Response
REST Web service: HTTP Status Code và xử lý ngoại lệ RESTful web service với Jersey 2.x
Wrapper Classes in Java
Dynamic Proxies in Java
Spring Cloud – Securing Services
How to Iterate Over a Stream With Indices
Java Program to Implement Segment Tree
Comparing Two HashMaps in Java
Instance Profile Credentials using Spring Cloud
Java Program to Implement Meldable Heap
Uploading MultipartFile with Spring RestTemplate
Java Program to Implement Variable length array
Logging a Reactive Sequence
How to Return 404 with Spring WebFlux
How to Count Duplicate Elements in Arraylist
Phương thức forEach() trong java 8
Adding a Newline Character to a String in Java
Java Program to Check Whether a Directed Graph Contains a Eulerian Path