Student Name: Kartik Guleria UID: 19BCS2241
Branch: BE-CSE Section/Group : CSE9/A
Semester: 5th
Subject Name: DAA LAB
Subject: 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;
}
19 bcs2241 daa_3.3

19 bcs2241 daa_3.3

  • 1.
    Student Name: KartikGuleria UID: 19BCS2241 Branch: BE-CSE Section/Group : CSE9/A Semester: 5th Subject Name: DAA LAB Subject: 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) {
  • 2.
    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;
  • 3.