jeudi 26 janvier 2023

Why does this code does not give result and keeps running?

->this code gonna work only when all characters in the pattern are distinct
->pattern matching question.

#include<iostream>
using namespace std;
void patSearching(string &text,string &pat)
{
    int n=text.length();
    int m=pat.length();
    for(int i=0;i<n-m;)
    {
        int j;
        for(j=0;j<m;j++)
        {
            if(pat[j]!=text[i+j])
              break;
              if(j==m)
              cout<<i<<" ";
              if(j==0)
              i++;
              else
              i=i+j;
        }
    }
}
int main()
{
    string text="ABCABCD";//it is the text against i try to match the pattern
    string pat="ABCD";//pattern
    patSearching(text,pat);
}

Even i tried to debug my code.But it doesn't work

Syntactically everything looks right compiler didn't give any error.

What can be the run time error here?

Aucun commentaire:

Enregistrer un commentaire