Brute force string matching problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sunilr245
    New Member
    • Oct 2013
    • 16

    Brute force string matching problem

    im supposed to write a code for fast string matching for class, i wrote the function find_string, and it worked for random strings i tested on my own but the test code doesnt output the correct answer, can anyone help as to why its not working? thank you

    Code:
    #include <iostream>
    #include <stdlib.h>
    #include <stdio.h>
    #define POSITION 89765
    
    
    using namespace std;
    
    int length(char const*s){
        int i = 0;
        while(s[i] != '\0'){++i;}
        return i;}
    
    int find_string(char *s, char *t)
    {
        int x = length(s);
        int y = length(t);
        int i;
    
        for(i=0;i<=(x-y);i++){
            int j=0;
            while((j<y) && (s[i+j] == t[j])){
                j++;
            }
            if(j==y){return i;}
        }
        return -1;
    }
    
    
    
    
    int main(void)
    
       {  char sequence[100001];
       char pattern[1000];
       int i, j;
       for(i=0; i<100000; i++)
          sequence[i] = 'a';
       sequence[100000]='\0';
       for(i=0; i<4000; i++)
       {  j = rand()%100000;
          sequence[j]='b';
       }
       for(j=0; j< 1000; j++)
          pattern[j] = sequence[POSITION+j];
       pattern[1000] = '\0';
       if(find_string(sequence, pattern) == POSITION )
         printf("accepted\n");
       else
         printf("needs check?\n");
    }
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    You don't need to know the lengths of the strings in order to compare them.

    Just start off comparing the first character of each string and make the following test (assumes you have a pointer to each string):

    if ((*strA == *strB) && (*strA != '\0')) then advance to compare the next character of each string.

    Otherwise,

    if (*strA < *str2) then string strA is less then string strB
    if (*strA > *strB) then strA is greater than string strB.
    Last edited by weaknessforcats; Dec 10 '13, 04:23 PM. Reason: typo in algorithm

    Comment

    • sunilr245
      New Member
      • Oct 2013
      • 16

      #3
      so what would my end condition for my for loop be if i dont know the length of the strings?

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        The loop runs until either *strA or *strB is \0.

        Code:
        while ((*strA == *strB) && (*strA != '\0'))
        {
            ++strA;
            ++strB;
        }
        //At this point you evaluate
        //One or both of strA and strB is \0
        
        if (*strA == *strB) return 0;  //the strings are equal
        if (*strA < *str2)  return -1; //strA is less then string strB
        return 1;  //strA s greater than strB

        Comment

        • sunilr245
          New Member
          • Oct 2013
          • 16

          #5
          i need the funtion to return the first position at which the substring starts. for example: if s = 'example' and t = 'ample'
          the function should return 2 and it should return -1 if there are no substrings of s

          Comment

          • weaknessforcats
            Recognized Expert Expert
            • Mar 2007
            • 9214

            #6
            OK. So use the loop in the example to locate a single matching character between the two strings. If there is one, then inside the loop compare starting at that location for the length of the substring (the mask in this case):

            Code:
            while (*strA == *strB)
             {
                 /* potential substring starts at strA  */
                 if (!strcmp(strA, strB))
                 {
                     /*then your substring starts a strA */ 
                 }
                 ++strA;
                 }
            /* if you get here no sub string was found  */

            Comment

            Working...