0% found this document useful (0 votes)
49 views

Naive String Matching

This Java program uses a naive string matching algorithm to search for occurrences of a pattern string within a main text string. It initializes data structures to store the pattern and pre-process the pattern characters. The search method iterates through the text, checking for matches between the pattern and text substrings and returning the index of the first match. The main method gets user input for the text and pattern, runs the search to find the first occurrence position, and prints the result.

Uploaded by

umar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
49 views

Naive String Matching

This Java program uses a naive string matching algorithm to search for occurrences of a pattern string within a main text string. It initializes data structures to store the pattern and pre-process the pattern characters. The search method iterates through the text, checking for matches between the pattern and text substrings and returning the index of the first match. The main method gets user input for the text and pattern, runs the search to find the first occurrence position, and prints the result.

Uploaded by

umar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

package com.sanfoundry.

setandstring;
import java.util.Scanner;
public class StringSearchUsingNaiveMethod
{
private final int BASE;
occurrence;
private int[]
pattern;
private String
public StringSearchUsingNaiveMethod(String pattern)
{
this.BASE = 256;
this.pattern = pattern;
occurrence = new int[BASE];
for (int c = 0; c < BASE; c++)
occurrence[c] = -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 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();
StringSearchUsingNaiveMethod nsm = new
StringSearchUsingNaiveMethod(
pattern);
int first_occur_position = nsm.search(text);
System.out.println("The text '" + pattern
+ "' is first found after the " + first_occur_position
+ " position.");
sc.close();
}
}

You might also like