You are on page 1of 3

Student Name: Tushar Parmar UID: 19BCS2626

Branch: BE_CSE Section/Group: 11 C


Semester: 5th Date of Performance:22/11/2021
Subject Name: DAA LAB Subject Code: CSP 309

Aim: Code and analyze to find all occurrences of a pattern P in a given string S.
Algorithms:

1. n ← length [T]
2. m ← length [P]
3. Π← COMPUTE-PREFIX-FUNCTION (P)
4. q ← 0
5. for i ← 1 to n
6. do while q > 0 and P [q + 1] ≠ T [i]
7. do q ← Π [q]
8. If P [q + 1] = T [i]
9. then q ← q + 1
10. If q = m
11. then print "Pattern occurs with shift" i - m
12. q ← Π [q]

Code:

#include <bits/stdc++.h>

using namespace std;

void search(char* pat, char* txt)

int M = strlen(pat);
int N = strlen(txt);

for (int i = 0; i <= N - M; i++) {

int j;

for (j = 0; j < M; j++)

if (txt[i + j] != pat[j])

break;

if (j == M)

cout << "Pattern found at index "

<< i << endl;

int main()

char txt[] = "Hello I Am Parent Class .";

char pat[] = "P";

search(pat, txt);

return 0;

You might also like