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:
A Guide to TreeSet in Java
Java 8 Stream findFirst() vs. findAny()
Java Program to Implement Rope
Java Program to Implement Segment Tree
Java Program to Perform Deletion in a BST
Summing Numbers with Java Streams
Java Program to Implement Bresenham Line Algorithm
Java Program to Implement Naor-Reingold Pseudo Random Function
Batch Processing with Spring Cloud Data Flow
Tiêu chuẩn coding trong Java (Coding Standards)
Java Program to Implement Rolling Hash
Guide To CompletableFuture
Java Program to Implement Strassen Algorithm
Guide to the Java ArrayList
Custom JUnit 4 Test Runners
Spring MVC Setup with Kotlin
Java Program to Implement Regular Falsi Algorithm
DynamoDB in a Spring Boot Application Using Spring Data
Giới thiệu luồng vào ra (I/O) trong Java
Java Program to do a Breadth First Search/Traversal on a graph non-recursively
Java Program to Find k Numbers Closest to Median of S, Where S is a Set of n Numbers
New Features in Java 9
Java Program to Implement PrinterStateReasons API
Convert String to Byte Array and Reverse in Java
Vector trong Java
Spring Boot - Google Cloud Platform
Java Program to Use rand and srand Functions
Java Program to Implement the One Time Pad Algorithm
Tổng quan về ngôn ngữ lập trình java
Introduction to Java Serialization
Guide to UUID in Java
A Quick Guide to Spring Cloud Consul