Java Program to Repeatedly Search the Same Text

This is a java program to repeatedly search a string from the sample string.

Here is the source code of the Java Program to Repeatedly Search the Same Text (such as Bible by building a Data Structure). The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

  1.  
  2. package com.sanfoundry.setandstring;
  3.  
  4. import java.util.Scanner;
  5.  
  6. class BoyerMooreAlgo
  7. {
  8.     private final int BASE;
  9.     private int[]     occurrence;
  10.     private String    pattern;
  11.  
  12.     public BoyerMooreAlgo(String pattern)
  13.     {
  14.         this.BASE = 256;
  15.         this.pattern = pattern;
  16.         occurrence = new int[BASE];
  17.         for (int c = 0; c < BASE; c++)
  18.             occurrence[c] = -1;
  19.         for (int j = 0; j < pattern.length(); j++)
  20.             occurrence[pattern.charAt(j)] = j;
  21.     }
  22.  
  23.     public void search(String text)
  24.     {
  25.         int n = text.length();
  26.         int m = pattern.length();
  27.         int skip = 0;
  28.         for (int i = 0; i < (n - m); i += skip)
  29.         {
  30.             skip = 0;
  31.             for (int j = m - 1; j >= 0; j--)
  32.             {
  33.                 if (pattern.charAt(j) != text.charAt(i + j))
  34.                 {
  35.                     skip = Math.max(1, j - occurrence[text.charAt(i + j)]);
  36.                     break;
  37.                 }
  38.             }
  39.             if (skip == 0)
  40.             {
  41.                 System.out.println("The text '" + pattern
  42.                         + "' is first found after the " + i + " position.");
  43.                 i++;
  44.             }
  45.         }
  46.         if (skip == 0)
  47.             System.out.println("The text '" + pattern
  48.                     + "' is first found after the " + n + " position.");
  49.     }
  50. }
  51.  
  52. public class ReperatedStringSearch
  53. {
  54.     public static void main(String[] args)
  55.     {
  56.         Scanner sc = new Scanner(System.in);
  57.         System.out.print("Enter the main string: ");
  58.         String text = sc.nextLine();
  59.         System.out.print("Enter the string to be searched: ");
  60.         String pattern = sc.nextLine();
  61.         BoyerMooreAlgo bm = new BoyerMooreAlgo(pattern);
  62.         bm.search(text);
  63.         sc.close();
  64.     }
  65. }

Output:

$ javac ReperatedStringSearch.java
$ java ReperatedStringSearch
 
Enter the main string: Sanfoundry is No. 1 choice for Deep Hands-ON Trainings in SAN, Linux & C, Kernel & Device Driver Programming. Our Founder has trained employees of almost all Top Companies in India. Here are few of them: VMware, Citrix, Oracle, Motorola, Ericsson, Aricent, HP, Intuit, Microsoft, Cisco, SAP Labs, Siemens, Symantec, Redhat, Chelsio, Cavium Networks, ST Microelectronics, Samsung, LG-Soft, Wipro, TCS, HCL, IBM, Accenture, HSBC, Northwest Bank, Mphasis, Tata Elxsi, Tata Communications, Mindtree, Cognizant, mid size IT companies and many Startups. Students from top Universities and colleges such as NIT Trichy, BITS Pilani, University of California, Irvine, University of Texas, Austin & PESIT Bangalore have benefited a lot from these courses as well. The assignments and real time projects for our courses are of extremely high quality with excellent learning curve.
Enter the string to be searched: C
The text 'C' is first found after the 71 position.
The text 'C' is first found after the 162 position.
The text 'C' is first found after the 212 position.
The text 'C' is first found after the 280 position.
The text 'C' is first found after the 324 position.
The text 'C' is first found after the 333 position.
The text 'C' is first found after the 397 position.
The text 'C' is first found after the 402 position.
The text 'C' is first found after the 425 position.
The text 'C' is first found after the 470 position.
The text 'C' is first found after the 496 position.
The text 'C' is first found after the 639 position.

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

advertisement
advertisement

Here’s the list of Best Books in Java Programming, Data Structures and Algorithms.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.