You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
38 lines
1.2 KiB
C++
38 lines
1.2 KiB
C++
/*This is a C++ Program to perform Naive String matching algorithm. In computer science, string searching algorithms, sometimes called string matching algorithms, are an important class of string algorithms that try to find a place where one or several strings (also called patterns) are found within a larger string or text.*/
|
|
|
|
#include<stdio.h>
|
|
#include<string.h>
|
|
void search(char *pat, char *txt)
|
|
{
|
|
int M = strlen(pat);
|
|
int N = strlen(txt);
|
|
/* A loop to slide pat[] one by one */
|
|
for (int i = 0; i <= N - M; i++)
|
|
{
|
|
int j;
|
|
/* For current index i, check for pattern match */
|
|
for (j = 0; j < M; j++)
|
|
{
|
|
if (txt[i + j] != pat[j])
|
|
break;
|
|
}
|
|
if (j == M) // if pat[0...M-1] = txt[i, i+1, ...i+M-1]
|
|
{
|
|
printf("Pattern found at index %d \n", i);
|
|
}
|
|
}
|
|
}
|
|
|
|
/* Driver program to test above function */
|
|
int main()
|
|
{
|
|
char *txt = "AABAACAADAABAAABAA";
|
|
char *pat = "AABA";
|
|
search(pat, txt);
|
|
return 0;
|
|
}
|
|
|
|
/*
|
|
Pattern found at index 0
|
|
Pattern found at index 9
|
|
Pattern found at index 13
|