私の課題は次のとおりです。
文字列マッチングでは、ある文字列 (パターン) の別の文字列 (テキスト) のインスタンスを探します。障害 (不一致) の数が最も少ないインスタンスを探しています。
たとえば、パターンが「camb」でテキストが「caammbd」の場合、「positions」を比較します。 1. 「camb」と「caam」を比較すると、2 つの不一致 (m と a、b と m) が示されます。2. "camb" と "aamm" を比較すると、2 つの不一致 (m と a、b と m) が示されます。等々....
私のプログラムは、ユーザーから 2 つの文字列を読み取り、最小の数字を見つけようとし、その「位置」が返されます。
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;
}
最悪のパターンは、不一致が最も少ない位置を表す必要があります。
ご想像のとおり、ここで何か問題が発生しました。デバッガーから、私の比較がうまくいっていないようです。ダバガーが示すように、文字列はよく読み取られます。
ご協力ありがとうございました。