0

My assignment is the following:

In string matching we are looking for an instance of one string (the pattern) in another (the text). We are looking for the instance, with the lowest number of faults (mismatches).

for exmaple, if the pattern is "camb" and the text is "caammbd" we compare "positions": 1. compare "camb" with "caam" which shows 2 mismatches (m with a and b with m). 2. compare "camb" with "aamm" which shows 2 mismatches (m with a and b with m). and so on....

My prgoram reads two strings from the user and tries to find the lowest number and its "position" is returned:

int misPattern(char str[], char pattern[]){
    int count = 0, maxCount = 0, worstPattern = 0, i = 0, j = 0, z = 0,
    patternNum = 1; 
    /*in order to make minimal tests, we must have each string's length*/
    int testsMade = 0;
    int numOfTests = (strlen(str, MAX_LEN + 1)) - (strlen(pattern, MAX_LEN + 1));
    while (str[i] != '\0' && testsMade<=numOfTests){
        z = i; count = 0;
        while (pattern[j] != '\0'){
            if (str[z] != pattern[j])
                count++;
            j++;
            z++;
        }
        j = 0;
        i++;
        if (count > maxCount){
            maxCount= count;
            worstPattern = patternNum;
        }
        patternNum++;
        testsMade++;
    }
    printf("%d\n", count);
    return worstPattern;
}

The worstPattern should represent the position that has the least mismatches.

As you may imagine, something went wrong here. It seems, from the debugger, that my comparisons are not going well. The strings are read well, as the dubugger shows.

Thank you for your help.

Assaf
  • 1,112
  • 4
  • 14
  • 35
  • 1
    [strlen()](http://linux.die.net/man/3/strlen) takes only one argument is string. Rather it should be `strnlen`. Also while loop logic fails if the test string and pattern are of same lengths. – Sunil Bojanapally Nov 09 '13 at 16:23
  • What do you want your algorithm to produce (I mean, what is it's expected result)? What the variable "worstPattern" should represent? – Luiz Vieira Nov 09 '13 at 16:24
  • the worstPattern should represent the position that has the most mismatches. @LuizVieira – Assaf Nov 09 '13 at 16:31
  • @SunEric Hi, this line seems to work just fine. I ran the numOfTests variable with the strings: "all" and "al" and the result was 1, the diefferrence in the strings' length. – Assaf Nov 09 '13 at 16:35
  • In that case, in your own example the position of most mismatches should be 3 right (i.e. matching "camb" with "mmbd")? Your code still produces 1. It seems you are counting the wrong thing. Shouldn't your code set the variable worstPattern = i instead of wostPattern = patternNum? – Luiz Vieira Nov 09 '13 at 16:42
  • I agree that I have something wrong, maybe one of my strings produces an extra space? but i is the variable for progressing through the string... @LuizVieira – Assaf Nov 09 '13 at 16:57
  • @LuizVieiraI think I found it. In the if I check str[i] instead of str[z]... and I think that's the problem. – Assaf Nov 09 '13 at 17:05

1 Answers1

0
int misPattern(char str[], char pattern[]){
    int count = 0, minCount = 0, worstPattern = 0, i = 0, j = 0, z = 0, patternNum = 0; 
    /*in order to make minimal tests, we must have each string's length*/
    int testsMade = 0;
    int numOfTests = (strlen(str, MAX_LEN + 1)) - (strlen(pattern, MAX_LEN + 1));

    /*if the pattern is longer
    than the text than return -1, and 
     if the strings are   `equal         also     return -1*/`
    if (strcmp(str,pattern)<=0)
        return -1;

    /*initial value for minCount. we have to make sure minCount is initilized at least once*/
    while (str[i] != '\0' && (minCount == 0) && testsMade<=numOfTests){
        z = i;
        while (pattern[j] != '\0'){
            if (str[z] != pattern[j])
                minCount++;
            j++;
            z++;
        }
        testsMade++;
        j = 0;
        i++;
        patternNum++;
    }

        printf("number of first minimal mismatches is %d\n", minCount);
        printf("i is %d j is %d\n", i, j);

    while (str[i] != '\0' && testsMade<=numOfTests){
        z = i; count = 0;
        while (pattern[j] != '\0'){
            if (str[z] != pattern[j])
                count++;

            j++;
            z++;
        }
        j = 0;
        i++;
        if (count < minCount){
            minCount= count;
            worstPattern = patternNum;
        }
        patternNum++;
        testsMade++;
    }

    return worstPattern;
}

This is my final program, after understanding my mistakes. Thank you all.

Assaf
  • 1,112
  • 4
  • 14
  • 35