Question

EDIT I rearranged the code and fixed the loop bugs.Now arrays are works fine but there is still one problem there.Loop iterates and finds the patterns in the string and then mark it.After that it keeps iterating and finds another same pattern in the text.Then marks it but while doing it, it resets the first marked pattern. Such that console output is like

OUTPUT

found at 3 to 6
hi
<mark>how</mark> are you
fine
how are you to
found at 20 to 23
hi
how are you
fine
<mark>how</mark> are you to

/How can i store the first array?I could not handle the const char array style/

/*I want the output like that:

found at 3 to 6
hi
<mark>how</mark> are you
fine
how are you to
found at 20 to 23
hi
<mark>how</mark> are you
fine
<mark>how</mark> are you to

I am very thankfull if you help


I want to do string matching with a brute force algorithm. I wrote the algorithm correctly, I think, and it works well. But I have one issue on it. The algorithm finds only one pattern even if the pattern is in the string many times. It only finds the first one. So that I try to call it over and over again in for loop. But I have one issue with that. When I try to call over and over again it messed up my array. In runtime I see some paths even if there are not in my array? What can be the reason for that?

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAX 200
  char *textCOOL;

  /* try to find the given pattern in the search string */
  int bruteForce(char *text, char *pattern,int stringlength,int patternlength) {
        char *text2;
        text2 = (char *) malloc(MAX);
        textCOOL = (char *) malloc(MAX);
        int i, j;
        int p=1;
        for (i = 0; i <= stringlength - patternlength; i++) {
            j=0;
            while (j < patternlength && pattern[j]==text[i+j]){

            j=j+1;

            if(j==patternlength ){
                int lentino;
                lentino=patternlength+(i);
                printf("from %d to %d i found\n",i,lentino);
                p=0;
                int start1;
                int start2;
                int start3;

                for(start1=0;start1<i;start1++){
                    text2[start1]=text[start1];
                }

                text2[i]='<';
                text2[i+1]='m';
                text2[i+2]='a';
                text2[i+3]='r';
                text2[i+4]='k';
                text2[i+5]='>';

                for(start2=0;start2<patternlength;start2++){
                    text2[(i+6+start2)]=pattern[start2];
                }
                text2[(i+6+patternlength)]='<';
                text2[(i+7+patternlength)]='/';
                text2[(i+8+patternlength)]='m';
                text2[(i+9+patternlength)]='a';
                text2[(i+10+patternlength)]='r';
                text2[(i+11+patternlength)]='k';
                text2[(i+12+patternlength)]='>';

                for(start3=0;start3<stringlength-1;start3++){

                    if(text[i+patternlength+start3]=='\0'){

                    text2[((i+13+patternlength)+start3)]='\0';
                    break;
                }
                    else{       
                    text2[((i+13+patternlength)+start3)]=text[i+patternlength+start3];
                }
            }       
                printf("%s",text2);

            }

            }
  }
   if(p==1){
                printf(" Not  found\n");    
            }
}

  int main() {
        char searchStr[MAX], pattern[MAX];
        int res;
        char j;
        FILE *fp;
        int start;
        int end;
        start=0;
        end=MAX;

        fp = fopen("deneme.txt", "r");

       if (fp == NULL) {
         printf("I couldn't open deneme.txt for writing.\n");
         exit(0);
      }
      int i;
        for(i=0; i<MAX; i++){
        j='\0';
        fscanf(fp,"%c",&j); 
        searchStr[i]=j;
}

      printf("%s\n",searchStr);

          fclose(fp);
        printf("Enter Pattern String:");
        fgets(pattern, MAX, stdin);
        searchStr[strlen(searchStr)-1] = '\0';
        pattern[strlen(pattern)-1] = '\0';
        res = bruteForce(searchStr, pattern, strlen(searchStr), strlen(pattern));

        return 0;

}

Was it helpful?

Solution

1) You are adding 13 bytes for every match you make but you are not tracking that you are staying in bounds of your strings.

2) The bruteForce for(start3 =... loop will not nul terminate text2. You stop copying when you hit a nul.

3) You have a lot of off-by-one errors, for example, when you are filling in textCOOL.

The idiom used in C for iterating over a string is:

for (i = 0; i < stringlength; i++)

Unless you actually mean to iterate over the nul

4) bruteForce should return an int but is not returning anything. You probably meant to return p

5) You should remove unused variables. It will help clarify your code.

6) You should free() the RAM you malloc() when you are done using it

7) It would be nicer to mark the text and pattern parameters as const and return a pointer to your malloc()ed string (NULL on no match)

8) In main() char text2 is assigned to malloc(). j is not a pointer and should not be assigned to malloc. that variable is never used again. Just delete it.

9) Be nice to your future self. Find more descriptive names than text$NUMBER

You are probably over-writing the nul-terminator of text2 in bruteForce and then when you strcpy() your results into text you are going to get whatever garbage data exists in RAM up until you are lucky enough to hit a 0 value. You seem to have file paths there.

EDIT:

Each time you have a match you copy the start of text into text2 up to the pattern match.

for (start1 = 0; start1 < i; start1++) {
    text2[start1]=text[start1];
}

Instead you need another variable to track where you are inside the destination string.

int dest = 0;

for (i = 0; i <= stringlength - patternlength; i++) {
    ...
            for(start1 = i-patternlength; start1 < i; start1++){
                text2[dest]=text[start1];
            }
            dest += sprintf(&text2[dest], "<mark>%s</mark>", pattern);
            printf("%s",text2);
    ...
}

Cheers

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top