-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathcode_1.cpp
45 lines (41 loc) · 973 Bytes
/
code_1.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
//
// code_1.cpp
// Algorithm
//
// Created by Mohd Shoaib Rayeen on 23/11/18.
// Copyright © 2018 Shoaib Rayeen. All rights reserved.
//
#include <iostream>
#include <string>
using namespace std;
bool search(string text , string pattern) {
long M = pattern.length();
long N = text.length();
bool flag = false;
for (int i = 0; i <= N - M; i++) {
int j;
for (j = 0; j < M; j++) {
if (text[i + j] != pattern[j]) {
break;
}
}
if (j == M) {
cout << "Pattern found at index\t:\t" << i << "\n";
flag = true;
}
}
return flag;
}
int main() {
string text , pattern;
cout << "\nEnter Text\t\t\t\t:\t";
getline(cin , text);
cout << "\nEnter Pattern\t\t\t:\t";
getline(cin , pattern);
cout << endl;
if ( !search(text ,pattern) ) {
cout << "\nPattern Not Found\n";
}
cout << endl;
return 0;
}