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 Implement the RSA Algorithm
Java Program to Implement LinkedHashSet API
Java Program to Represent Graph Using 2D Arrays
How To Serialize and Deserialize Enums with Jackson
Java Program to Print the Kind of Rotation the AVL Tree is Undergoing
Java Program to Implement Variable length array
Java Program to Implement Floyd Cycle Algorithm
Java Program to Implement Sieve Of Eratosthenes
Using a Spring Cloud App Starter
A Quick JUnit vs TestNG Comparison
Spring Boot - Actuator
Java Program to Generate All Pairs of Subsets Whose Union Make the Set
Java Program to Implement Graph Coloring Algorithm
Java Program to Find Inverse of a Matrix
Array to String Conversions
Java Program to implement Array Deque
Java Program to Implement Queue using Two Stacks
Java Program to Compute Discrete Fourier Transform Using the Fast Fourier Transform Approach
Guide to the ConcurrentSkipListMap
Java Program to Find the Nearest Neighbor Using K-D Tree Search
Spring Boot - Thymeleaf
Java Program to Implement Sorted Doubly Linked List
An Introduction to ThreadLocal in Java
Introduction to Eclipse Collections
Java Program to Implement Euler Circuit Problem
Java Program to implement Bit Matrix
String Initialization in Java
Guide To CompletableFuture
String Operations with Java Streams
A Guide to Java HashMap
Testing an OAuth Secured API with Spring MVC
How to Return 404 with Spring WebFlux