Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions Searching algorithm
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// C++ program for Naive Pattern
// Searching algorithm
#include <bits/stdc++.h>
using namespace std;

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]
cout << "Pattern found at index " << i << endl;
}
}

// Driver's Code
int main()
{
char txt[] = "AABAACAADAABAAABAA";
char pat[] = "AABA";

// Function call
search(pat, txt);
return 0;
}