OneCompiler

Pattern matching

1625

#include <iostream>
#include <cctype>
#include <string>
using namespace std;

string toLowerCase(const string &str) {
string lowerStr = str;
for (int i = 0; i < lowerStr.size(); i++) {
lowerStr[i] = tolower(lowerStr[i]);
}
return lowerStr;
}

void patternMatching(const string &txt, const string &pattern) {
int txtSize = txt.size();
int patternSize = pattern.size();
bool found=false;

for (int i = 0; i <= txtSize - patternSize; i++) {
    int j;
    for (j = 0; j < patternSize; j++) {
        if (txt[i + j] != pattern[j]) {
            break;
        }
    }
    if (j == patternSize) {
        cout<<"Pattern Found at "<<(i+1)<<endl;
        found=true;
    }
}
if(found==false)
{
  cout<<"Pattern Not Found"<<endl;
}

}

int main() {
string txt;
getline(cin, txt);

string pattern;
getline(cin, pattern);


txt = toLowerCase(txt);
pattern = toLowerCase(pattern);


patternMatching(txt, pattern);

return 0;

}