Java Program to Perform Naive String Matching

This is a java program to perform Naive String matching algorithm.

Here is the source code of the Java Program to Perform Naive String Matching. 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. public class StringSearchUsingNaiveMethod
  7. {
  8.     private final int BASE;
  9.     private int[]     occurrence;
  10.     private String    pattern;
  11.  
  12.     public StringSearchUsingNaiveMethod(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 int search(String text)
  24.     {
  25.         int n = text.length();
  26.         int m = pattern.length();
  27.         int skip;
  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.                 return i;
  41.         }
  42.         return n;
  43.     }
  44.  
  45.     public static void main(String[] args)
  46.     {
  47.         Scanner sc = new Scanner(System.in);
  48.         System.out.print("Enter the main string: ");
  49.         String text = sc.nextLine();
  50.         System.out.print("Enter the string to be searched: ");
  51.         String pattern = sc.nextLine();
  52.         StringSearchUsingNaiveMethod nsm = new StringSearchUsingNaiveMethod(
  53.                 pattern);
  54.         int first_occur_position = nsm.search(text);
  55.         System.out.println("The text '" + pattern
  56.                 + "' is first found after the " + first_occur_position
  57.                 + " position.");
  58.         sc.close();
  59.     }
  60. }

Output:

$ javac StringSearchUsingNaiveMethod.java
$ java StringSearchUsingNaiveMethod
 
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: No. 1 
The text 'No. 1 ' is first found after the 14 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.